.NET Zone is brought to you in partnership with:

Dror Helper is an experienced software developer has written and designed software in various fields including video streaming, eCommerce, performance optimization and unit testing tools. He is passionate about programming best practices and all things software development, and has been a guest presenter at several user group meetings and ALT.NET events. Dror's blog can be found at http://blog.drorhelper.com where he writes about unit testing, agile methodologies, development tools, programming languages and anything else he finds interesting. Dror is a DZone MVB and is not an employee of DZone and has posted 54 posts at DZone. You can read more from them at their website. View Full User Profile

Why I Like C++ 11 – auto & nullptr

06.23.2013
| 2618 views |
  • submit to reddit

I’ve been away from C++ for the last three years, this is the longest we’ve been apart.
Luckily I’ve been given a chance to sharpen my skills and in the last few weeks I’ve been working exclusively in C++. 
During our time apart C++ has grown, and changed – this is not your father’s C++ it’s the new and improved C++ 11!
In fact C++ 11 seems like a completely different language – and I’m not the only one that think that:

C++11 feels like a new language: The pieces just fit together better than they used to and I find a higher-level style of programming more natural than before and as efficient as ever. 
Bjarne Stroustrup from C++ 11 FAQ

So I invite you to join me as I re-discover C++. Today I’ll discuss two new keywords in C++ 11 – auto andnullptr:

auto

auto enable us to write code that looks like this:

auto i = 5;    // I'm an integer
auto s = "text";    // I'm a char*
auto vec = new vector<double>(); // and I'm vector<double>*

Did C++ just became dynamic? Of course not!
Auto is similar to C#’s var keyword – it tells the compiler to go ahead and figure out what type I want you to use – because the compiler works for us and not the other way around.
Although I’ve seen auto (and var) abused if used correctly – this simple trick can save you some time  especially when using with lambdas (new feature) or just plain Iterator<x<y<z … which happen from time to time.

nullptr

No much to tell about this – but I’ll try: nullptr replace NULL when comparing or setting pointer values. The big difference is that NULL of old was plain ‘0’ (zero) in disguise causing funny (not “ha-ha” funny)  behavior from 0 == NULL to accidently calling the wrong overload of a method – func(int) vs. func(MyClass*).
Using nullptr (of type std::nullptr_t) – if you want more details have a look at this StackOverflow question.

So far so good

Is this all – not by a long shot. C++11 have many other goodies – which I plan to write about in future blog posts.


Happy coding…

Published at DZone with permission of Dror Helper, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)