본문 바로가기

Software/C/C++

[C++]Copy Constructor vs = operator 복사 생성자

복사 생성자 호출 조건, there are several condition for call copy constructor
1.pass by value object
2.return by value object
3.initialize by value object
4.
5.

if there is a copy constructor and a =operator
classA::classA(const classA& aa){.....}
classA& calssA::operator=(const classA& aa){....}

in source
classA aa(1,"KK");
classA ab, bb = aa;  //1
ab = aa;                 //2

in line 1, bb=aa it calls copy constructor no =,
bb is initialized by aa. but, line 2 calls = operator because ab instance is already created in line 1.