Is there any way I can use make_shared
instead of shared_ptr
for abstract types?
Example:
#include <memory>
#include <vector>
class Foo
{
public:
virtual void fooFunc() = 0;
};
class Bar : public Foo
{
public:
void fooFunc() {};
};
int main()
{
std::vector< std::shared_ptr<Foo> > vec;
vec.push_back(std::shared_ptr<Foo>(new Bar()));
//vec.push_back(std::make_shared<Foo>(new Bar())); //doesn't work because Foo is abstract
return 0;
}
I ended up here because I didn't specify my inheritance as public. So I had:
class Bar : Foo
instead of:
class Bar : public Foo
Adding the public made shared_ptr<Foo> foo = make_shared<Bar>(...)
work as expected
You may use a std::static_pointer_cast
if you want to be explicitly mentioning the cast:
int main()
{
std::vector<std::shared_ptr<Foo>> vec;
vec.push_back(std::static_pointer_cast<Foo>(std::make_shared<Bar>()));
}
But it also works without it:
vec.push_back(std::make_shared<Bar>());
Here's a demo showing the behaviour: Demo
I'd recommend not to use std::make_shared
in combination with new
. There is a performance concern here and in my opinion it's a good thing to avoid unbalanced new
/ delete
s.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With