Chapter 36. Initialization

Difficulty: 3

What's the difference between direct initialization and copy initialization, and when are they used?

  1. What is the difference between direct initialization and copy initialization?

  2. Which of the following cases uses direct initialization, and which uses copy initialization?

     class T : public S { public: T() : S(1), // base initialization x(2) {} // member initialization X x; }; T f( T t ) // passing a function argument { return t; // returning a value } S s; T t; S& r = t; reinterpret_cast<S&>(t); // performing a reinterpret_cast static_cast<S>(t); // performing a static_cast dynamic_cast<T&>(r);// performing a dynamic_cast const_cast<const T&>(t); // performing a const_cast try { throw T(); // throwing an exception ...

Get More Exceptional C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.