如果我声明一个包装在共享指针中的对象:
std::shared_ptr<myClass> myClassObject(new myClass());
Run Code Online (Sandbox Code Playgroud)
然后我想将它作为参数传递给方法:
DoSomething(myClassObject);
//the called method
void DoSomething(std::shared_ptr<myClass> arg1)
{
arg1->someField = 4;
}
Run Code Online (Sandbox Code Playgroud)
以上只是增加了shared_pt的引用计数,一切都很酷吗?还是留下一个悬垂的指针?
你还是应该这样做吗?:
DoSomething(myClassObject.Get());
void DoSomething(std::shared_ptr<myClass>* arg1)
{
(*arg1)->someField = 4;
}
Run Code Online (Sandbox Code Playgroud)
我认为第二种方式可能更有效,因为它只需要复制1个地址(而不是整个智能指针),但第一种方式似乎更具可读性,我不期望推动性能限制.我只是想确保没有危险的东西.
谢谢.
我在整个应用程序中广泛使用std :: tr1 :: shared_ptr.这包括在函数参数中传递对象.考虑以下:
class Dataset {...}
void f( shared_ptr< Dataset const > pds ) {...}
void g( shared_ptr< Dataset const > pds ) {...}
...
Run Code Online (Sandbox Code Playgroud)
虽然通过shared_ptr传递数据集对象可以保证它在f和g中的存在,但是这些函数可能会被调用数百万次,这会导致很多shared_ptr对象被创建和销毁.这是最近一次运行的平坦gprof配置文件的片段:
Each sample counts as 0.01 seconds. % cumulative self self total time seconds seconds calls s/call s/call name 9.74 295.39 35.12 2451177304 0.00 0.00 std::tr1::__shared_count::__shared_count(std::tr1::__shared_count const&) 8.03 324.34 28.95 2451252116 0.00 0.00 std::tr1::__shared_count::~__shared_count()
因此,大约17%的运行时用于使用shared_ptr对象进行引用计数.这是正常的吗?
我的应用程序的很大一部分是单线程的,我正在考虑重写一些函数
void f( const Dataset& ds ) {...}
Run Code Online (Sandbox Code Playgroud)
并替换电话
shared_ptr< Dataset > pds( new Dataset(...) );
f( pds …Run Code Online (Sandbox Code Playgroud) 在他的Blog Herb Sutter写道
[...]因为增加智能指针引用计数 通常可以优化为与优化
shared_ptr实现中的普通增量相同- 在生成的代码中只是普通的增量指令,而不是围栏.然而,减量必须是原子减量或等效物,它产生特殊的处理器存储器指令,这些指令本身更昂贵,并且最重要的是在优化周围代码时引起存储器栅栏限制.
该文是关于执行的shared_ptr,我不确定他的评论是否只适用于此或通常是这样.从他的表述我收集它一般.
但是当我想到它时,我只能想到"更加昂贵的减量",当if(counter==0)紧接着 - 这可能就是这种情况shared_ptr.
因此,我想知道原子操作++counter是否(通常)总是快于--counter,或者只是因为它if(--counter==0)...与shared_ptr?一起使用?
shared_ptr<drived>当我将计数器传递给需要a的函数时,为什么计数器会增加const shared_ptr<base>&?
在这个问题中,答案之一是:
shared_ptr<Base> and shared_ptr<Derived> are not covariant
Run Code Online (Sandbox Code Playgroud)
我怀疑这与我的问题有关。它们不是协变是什么意思?
这是一个代码片段,用于展示这种情况:
#include <iostream>
#include <memory>
class Base {};
class Derived : public Base {};
void f(const std::shared_ptr<Base>& x)
{
std::cout << "in function expecting const shared_ptr<Base>& - Use count: " << x.use_count() << std::endl;
}
int main(int argc, char const *argv[])
{
std::cout << "Base class" << std::endl;
auto a = std::make_shared<Base>();
std::cout << "Created shared_ptr: Initial use count: " << a.use_count() << std::endl;
f(a); …Run Code Online (Sandbox Code Playgroud) 如何将派生自接口的类传递给以接口为参数的函数?
我有一个接口和一个类设置类似这样的东西。
class Interface
{
public:
virtual ~Interface() {}
virtual void DoStuff() = 0;
};
class MyClass : public Interface
{
public:
MyClass();
~MyClass();
void DoStuff() override;
};
void TakeAnInterface(std::shared_ptr<Interface> interface);
int main()
{
auto myInterface = std::make_shared<MyClass>();
TakeAnInterface(myInterface);
}
Run Code Online (Sandbox Code Playgroud)
编译器抱怨No matching function call to TakeAnInterface(std::shared_ptr<MyClass>&)。为什么功能TakeAnInterface不能接收Interface类而不是MyClass?
c++ ×5
shared-ptr ×3
c++11 ×2
performance ×2
atomic ×1
c++-faq ×1
interface ×1
polymorphism ×1