doomblah
doomblah

Reputation: 77

How to initialise a shared pointer that is a class member in the constructor

I have a shared pointer but am not able to initialise it to 0.

Here is my code:

class ROSThreadObjTest
{
private:
    std::shared_ptr<int> var;
    std::vector<ROSThreadObj> threads;
    std::vector<void *(*)(void *)> functions;

public:

    ROSThreadObjTest():(new int(0))
    {
    }

How can I modify my constructor to set the value of var to 0 when the constructor is called?

Upvotes: 0

Views: 754

Answers (1)

Cory Kramer
Cory Kramer

Reputation: 117856

If you want to allocate a new pointer and set that int to 0 then you can use std::make_shared

ROSThreadObjTest() : var(std::make_shared<int>(0)) {}

If rather you want to set the pointer to nullptr (if that's what you meant by 0), then you don't have to do anything special in the constructor, as that is what the shared_ptr will be default initialized to.

Upvotes: 3

Related Questions