Rom*_*kov 3 c++ initialization declaration
请考虑以下声明和初始化类型变量的方法C:
C c1;
C c2;
c2 = C();
C c3(C());
C c4 = C();
Run Code Online (Sandbox Code Playgroud)
所有这些都完全相同,或者根据确切的定义,其中一些是否有所不同C?(假设它有公共默认值和复制构造函数).
小智 10
这些意思是:
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
Run Code Online (Sandbox Code Playgroud)
请注意,编译器可以忽略复制构造函数调用.它们是等价的吗? - 好吧,这取决于复制构造函数和赋值运算符的作用.