Max*_*rai 2 c++ boost pointers smart-pointers
class Object { /* */ };
Run Code Online (Sandbox Code Playgroud)
和一些派生的:
class Derived1 : public Object { /* */ };
class Derived2 : public Object { /* */ };
Run Code Online (Sandbox Code Playgroud)
我有一个函数,它使派生对象和返回指针Object;
Object *make()
{
return new Derived1();
}
Run Code Online (Sandbox Code Playgroud)
所以,这种方式我必须通过智能指针包装返回的对象,但要使用什么返回类型?
TYPE? make()
{
return boost::shared_ptr<Derived1>(new Derived1());
}
Run Code Online (Sandbox Code Playgroud)
您可以使用:
boost::shared_ptr<Object> make()
{
return boost::shared_ptr<Object>(new Derived1());
}
Run Code Online (Sandbox Code Playgroud)