Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost Shared_pointer NULL

I'm using reset() as a default value for my shared_pointer (equivalent to a NULL).

But how do I check if the shared_pointer is NULL?

Will this return the right value ?

boost::shared_ptr<Blah> blah; blah.reset() if (blah == NULL)  {     //Does this check if the object was reset() ? } 
like image 235
Yochai Timmer Avatar asked Apr 10 '11 08:04

Yochai Timmer


People also ask

Can shared_ ptr be null?

A null shared_ptr does serve the same purpose as a raw null pointer. It might indicate the non-availability of data. However, for the most part, there is no reason for a null shared_ptr to possess a control block or a managed nullptr .

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.


2 Answers

Use:

if (!blah) {     //This checks if the object was reset() or never initialized } 
like image 92
Ralph Avatar answered Oct 11 '22 04:10

Ralph


if blah == NULL will work fine. Some people would prefer it over testing as a bool (if !blah) because it's more explicit. Others prefer the latter because it's shorter.

like image 32
ymett Avatar answered Oct 11 '22 04:10

ymett