Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
680 views
in Technique[技术] by (71.8m points)

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

(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).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...