Jump to content
  • Advertisement
Sign in to follow this  
RonnieCassinello

C++11 'nullptr' - does it convert to 'false' implicitly?

This topic is 2641 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all,

I'm currently starting a new project to just mess about with inheritance and polymorphism (something I've not even looked at for a good 10 years), and I'd like my code to use the new nullptr keyword.

The problem I have is that my compiler doesn't actually support it yet, so as a hack I've, defined it as a const int, like so:
const int nullptr = 0;...for the time being.

This has left me curious as to whether my code will be broken when I do get a compiler that supports nullptr - will nullptr implicity convert to 'false'?

Perhaps it's best with an example. Say I had a pointer to an object, which may contain nullptr or may contain a valid address. Would the following snippet still work?


if (pObject)
{
pObject->someFunction();
// and other stuff
}
else
{
pObject = new Object;
// and other stuff
}


Simlarly, if I had a vector of pointers, some of which are set to nullptr, could I still use this to delete the objects in a class destructor?


for (unsigned int i = 0; i < pObjects.size(); ++i) if (pObject) delete pObject;


Many thanks,
Ronnie

Share this post


Link to post
Share on other sites
Advertisement
Yes, nullptr evaluates to false. It can pretty much be used as a drop-in replacement for 0 (or NULL if you prefer). I have yet to come across any problems with nullptr that forced me to revert to 0, even when passing it to third-party librarries (using VS 2010)

However, if your taking the plunge into modern C++ you shouldn't be messing with raw pointers at all. Instead learn to love the smart pointer, such as the shared_ptr and the weak_ptr. They will make your life much more enjoyable smile.gif If your compiler doesn't support nullptr, it probably doesn't have builtin smart pointers except std::auto_ptr (which has a bit tricky semantics), but look into Boost.SmartPtr.

Share this post


Link to post
Share on other sites
I haven't used nullptr myself yet but as I understand it nullptr is only used to distinguish from the integer literal 0. As soon as you assign it to a pointer it will be just a normal null pointer.

Share this post


Link to post
Share on other sites
I have recently started a new project in VS2010, using much of the C++11 features available there (still waiting for the new for loops and {} initialization, seems slick) and i have yet too come up with a situation where nullptr doesn't work like before. However:



for (unsigned int i = 0; i < pObjects.size(); ++i) if (pObject) delete pObject;


Many thanks,
Ronnie


It is valid C++ (and has always been) to delete null pointers (it does noting), so the if (pObject) is kind of redundant, if that is an actual example from your code. :)

Share this post


Link to post
Share on other sites
Thank you both for the quick replies. That's great!

As for the Boost library, that's on my 'to do' list of things to look at. At the moment, I'm working my way back through Ivor Horton's Beginning C++ (first edition and positively battered to bits!), and sighing at how much I've forgotten, whilst also applying it to simple projects (and firmly blurring the line between using c and c++ for good measure).

But yes, I've heard so many good things about Boost that I definitely want to learn anout it. As comfortable as I am using pointers, I still drop the ball from time to time.

Thanks again,
Ronnie

Share this post


Link to post
Share on other sites
Consider using this version of nullptr:

http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/nullptr#Solution_and_Sample_Code

The whole point of the new nullptr keyword is to provide a type safe null that won't accidentally be used in an integer expression. Anyone using nullptr with your version of it, expecting said type safety, will be in for a horrible surprise.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!