问题
(Using Visual Studio 2010) I'm trying to create a shared_ptr of an existing class in my project (class was written a decade before std::shared_ptr existed). This class takes a non-const pointer to another object, it's empty parameter constructor is private.
class Foobar {
public:
Foobar(Baz* rBaz);
private:
Foobar();
}
When I try to create a shared_ptr to it, things don't go well:
Baz* myBaz = new Baz();
std::shared_ptr<Foobar> sharedFoo = std::make_shared<Foobar>(new Foobar(myBaz));
On VS2010, this gives me
error C2664: 'Foobar::Foobar(const Foobar &)' : cannot convert parameter 1 from 'Foobar *' to 'const Foobar &'
3> Reason: cannot convert from 'Foobar *' to 'const Foobar'
For some reason it appears to be calling the copy constructor of Foobar
instead of the constructor that takes a Baz*
.
I'm also not sure about the cannot convert from 'Foobar *' to 'const Foobar'
part. My best interpretation is that my templated-type of shared_ptr<Foobar>
is wrong. I made it shared_ptr<Foobar*>
but this seems wrong, all examples I've seen don't make the type a raw pointer.
It seems that making everything shared_ptr<Foobar*>
compiles properly, but will that prevent the Foobar
object from getting deleted properly when all shared_ptr
's go out of scope?
Edit: This seems related, but I'm not using Boost: boost make_shared takes in a const reference. Any way to get around this?
Edit2: For clarity, if you're wondering why I'm using make_shared()
, in my actual code the sharedFoo
variable is a class member of a third class (independent of Foobar
and Baz
).
回答1:
That should be;
std::shared_ptr<Foobar> sharedFoo = std::make_shared<Foobar>(myBaz);
...since make_shared constructs the actual object for you by running the constructor.
来源:https://stackoverflow.com/questions/8789636/errors-in-stdmake-shared-when-trying-to-make-shared-ptr