Implementing the copy constructor in terms of operator=

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

    While the end result is the same, the members are first default initialized, only copied after that.

    With 'expensive' members, you better copy-construct with an initializer list.

    struct C {
       ExpensiveType member;
    
       C( const C& other ): member(other.member) {}
    };
    
    
    
     };
    

提交回复
热议问题