Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 807
Bret Kuhns Avatar asked Jan 09 '12 14:01

Bret Kuhns


People also ask

What is the difference between make_shared and shared_ptr?

The difference is that std::make_shared performs one heap-allocation, whereas calling the std::shared_ptr constructor performs two.

Should I use make_shared?

make_shared is exception-safe. It uses the same call to allocate the memory for the control block and the resource, which reduces the construction overhead. If you don't use make_shared , then you have to use an explicit new expression to create the object before you pass it to the shared_ptr constructor.

What happens when shared_ptr goes out of scope?

The smart pointer has an internal counter which is decreased each time that a std::shared_ptr , pointing to the same resource, goes out of scope – this technique is called reference counting. When the last shared pointer is destroyed, the counter goes to zero, and the memory is deallocated.

What is the purpose of the shared_ptr <> template?

std::shared_ptr. std::shared_ptr is a smart pointer that retains shared ownership of an object through a pointer. Several shared_ptr objects may own the same object.


1 Answers

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.

like image 60
Joachim Isaksson Avatar answered Sep 20 '22 22:09

Joachim Isaksson