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.
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.
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.
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++).
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().
#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
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>())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With