Declarations and C ++ Variable Initialization Rules

Consider the following ways to declare and initialize a type variable C:

C c1;

C c2;
c2 = C();

C c3(C());

C c4 = C();

Are they all completely equivalent to each other or may differ depending on the exact definition C? (provided that it has public defaults and copy constructors).

+3
source share
1 answer

It means:

C c1;   // default constructor

C c2;   // default constructor
c2 = C(); // default constructor followed by assignment

C c3(C());   // default constructor possibly followed by copy constructor

C c4 = C();  // default constructor possibly followed by copy constructor

Note that the compiler can make calls to the copy constructor. Are they equivalent? - well, it depends on what the copy constructor and assignment operator do.

+10
source

Source: https://habr.com/ru/post/1757849/


All Articles