Errors in std::make_shared() when trying to make shared_ptr?

不打扰是莪最后的温柔 提交于 2019-11-28 07:53:18

问题


(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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!