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.
精彩评论