开发者

copy constructor in object declaration and return from function

开发者 https://www.devze.com 2023-03-18 18:46 出处:网络
can\'t we simply overload \"=\" operator to instead of using copy constructor in object declaration andreturn from function ie in

can't we simply overload "=" operator to instead of using copy constructor in object declaration and return from function ie in

someclass X = Y;
X=somefunction();

can I overload "=" operator in someclas开发者_高级运维s and not provide a copy constructor.

Is there any case which requires both copy constructor and overloading "=" operator to avoid pointer variables from two seperate objects to point to same memory location?


No, those are different concepts and both of them exist for performance reasons.

  • Copy constructor is used when you create a new object (from an existing object).
  • Assignment is used when the object already exists but you want to assign a new value to it.

If you provide one of them provide the other one as well.

0

精彩评论

暂无评论...
验证码 换一张
取 消