Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default value for boost::shared_ptr on class constructor

Suppose I have class like

class A{
    public:
    A(int a, boost::shared_ptr<int> ptr){
        // whatever!
    }
};

My question is, what's the default value for that ptr? I'd like to be able to create an instance of that class using

A myA(5);

Sure I know I could create another constructor with just one parameter, but I'm looking for something like

A(int a, boost::shared_ptr<int> ptr = WAT?)

Is it possible? Currently I'm using the two-constructors way, but it would be great to do it this way.

like image 433
José Tomás Tocino Avatar asked Dec 20 '10 02:12

José Tomás Tocino


People also ask

What is boost :: shared_ptr?

shared_ptr is now part of the C++11 Standard, as std::shared_ptr . Starting with Boost release 1.53, shared_ptr can be used to hold a pointer to a dynamically allocated array. This is accomplished by using an array type ( T[] or T[N] ) as the template parameter.

What is the purpose of the shared_ptr <> template?

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.

Where is std :: shared_ptr defined?

If your C++ implementation supports C++11 (or at least the C++11 shared_ptr ), then std::shared_ptr will be defined in <memory> . If your C++ implementation supports the C++ TR1 library extensions, then std::tr1::shared_ptr will likely be in <memory> (Microsoft Visual C++) or <tr1/memory> (g++'s libstdc++).

Does shared_ptr delete?

the last remaining shared_ptr owning the object is destroyed. the last remaining shared_ptr owning the object is assigned another pointer via operator= or reset().


2 Answers

#include <boost/make_shared.hpp>

A(int a, boost::shared_ptr<int> ptr = boost::make_shared<int>())

Check http://www.boost.org/doc/libs/1_43_0/libs/smart_ptr/make_shared.html

like image 96
OneOfOne Avatar answered Nov 05 '22 07:11

OneOfOne


I finally found it here, I can use the shared pointer's default constructor like this:

A(int a, boost::shared_ptr<int> ptr = boost::shared_ptr<int>())
like image 28
José Tomás Tocino Avatar answered Nov 05 '22 07:11

José Tomás Tocino