开发者

copy constructor in object declaration and return from function

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜