Implementing the copy constructor in terms of operator=

后端 未结 8 1945
自闭症患者
自闭症患者 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:44

    It is more advisable to implement operator= in terms of an exception safe copy constructor. See Example 4. in this from Herb Sutter for an explanation of the technique and why it's a good idea.

    http://www.gotw.ca/gotw/059.htm

提交回复
热议问题