Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Derived classes and smart pointers

class Object { /* */ };

and a few derived:

class Derived1 : public Object { /* */ };
class Derived2 : public Object { /* */ };

And I have a function which makes derived objects and returns pointer for Object;

Object *make()
{
   return new Derived1();
}

So, this way I have to wrap returned object by smart pointer, but what return type to use?

TYPE? make()
{
   return boost::shared_ptr<Derived1>(new Derived1());
}
like image 344
Max Frai Avatar asked Nov 26 '25 06:11

Max Frai


1 Answers

you can use:

boost::shared_ptr<Object> make()
{
return boost::shared_ptr<Object>(new Derived1());
}
like image 193
bcsanches Avatar answered Nov 27 '25 21:11

bcsanches



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!