Here is a small piece of code:
class A { public: A(int value) : value_(value) { cout <<"Regular constructor" <<endl; } A(const A& other) : value_(other.value_) { cout <<"Copy constructor" <<endl; } private: int value_; }; int main() { A a = A(5); }
I assumed that the output would be "Regular Constructor" (for RHS), followed by a "Copy constructor" for LHS. Therefore, I avoided this style and always declared a class variable as A a(5); . But, to my surprise, in the code above, the copy constructor is never called (Visual C ++ 2008)
Does anyone know if this behavior is the result of optimizing the compiler or some documented (and portable) C ++ function? Thank.
c ++ constructor copy-constructor
BostonLogan Nov 18 '09 at 18:40 2009-11-18 18:40
source share