O'Reilly logo

C++ For Dummies®, 6th Edition by Stephen R. Davis

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 29. Ten Major Recent Additions to C++

In This Chapter

  • Using smart pointers

  • Initializing objects with a variable-length list

  • Initializing data members inline

  • Instantiating an extern template

  • Implementing thread local storage

  • Using r-value references to avoid creating copies of objects

  • Implementing concepts

  • Defining lamda expressions

  • Defining variadic templates

  • Using typeid

Note

In this chapter, I look at ten major additions to the C++ language. Most (but not all) of these additions weren't made formal until the release of the C++ 2009 standard. All of these topics are beyond the range of an introductory book; I merely touch on these topics to give you an idea of where you might want to study next.

Use Smart Pointers

Loss of memory due to forgetting to return memory to the heap is one of the biggest sources of error in C++ programs. Both Java and C#, two followers to the C++ language, instituted a process known as garbage collection that collects unreferenced memory and returns it to the heap for you. Garbage collection has been discussed for C++ as well but has been rejected for several reasons, including the overhead it levies on your program.

The problem could be "solved" if dynamically allocated memory were tied to an object rather than a pointer. The destructor of an object is invoked when it goes out of scope. This destructor could keep track of when it was appropriate to return the memory to the heap.

The standard library includes a shared_ptr<T> template class, which allocates memory ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required