Implementing the copy constructor in terms of operator=

后端 未结 8 1948
自闭症患者
自闭症患者 2020-12-28 16:19

If the operator= is properly defined, is it OK to use the following as copy constructor?

MyClass::MyClass(MyClass const &_copy)
{
    *this          


        
8条回答
  •  盖世英雄少女心
    2020-12-28 16:43

    yes.

    personally, if your class doesn't have pointers though I'd not overload the equal operator or write the copy constructor and let the compiler do it for you; it will implement a shallow copy and you'll know for sure that all member data is copied, whereas if you overload the = op; and then add a data member and then forget to update the overload you'll have a problem.

提交回复
热议问题