简体   繁体   English

在复制赋值运算符中分配基类成员

[英]Assigning base class members in copy assignment operator

I've got a class that inherits from a MSFT class, and therefore cannot be changed, and I'd like my derived class to have identical behavior for its copy constructor and copy assignment operator. 我有一个继承自MSFT类的类,因此无法更改,我希望我的派生类对其复制构造函数和复制赋值运算符具有相同的行为。 The trouble I'm having is that in the copy constructor, you are free to invoke a constructor for the base class in the initializer list, but in the operator, this is not an option. 我遇到的麻烦是,在复制构造函数中,您可以在初始化列表中为基类调用构造函数,但在运算符中,这不是一个选项。 How can I properly recreate this behavior in the assignment operator? 如何在赋值运算符中正确地重新创建此行为? Is it sufficient to just call the base class's constructor in the body of the operator overload? 仅仅在运算符重载的主体中调用基类的构造函数就足够了吗?

Additional note: the base class inherits from CObject, which has operator=() and the copy constructor as private and unimplemented methods, so unfortunately any calls to those will result in a compile error. 附加说明:基类继承自CObject,它具有operator =(),复制构造函数作为私有和未实现的方法,所以不幸的是,对这些方法的任何调用都将导致编译错误。

I've provided a simplified code scenario below: 我在下面提供了一个简化的代码方案:

Class declarations: 类声明:

class Base    
{
protected:
    int baseInt;
public:
    Base(int);
}

class Derived : public Base
{
public:
    Derived(const Derived& other);
    Derived& operator=(const Derived& rhs);
private:
    int derivedInt;
}

Derived class member functions: 派生类成员函数:

// Copy Constructor
Derived(const Derived& other) : Base(5)
{
    derivedInt = other.derivedInt;
}

// Copy Assignment Operator
Derived& operator=(const Derived& rhs)
{
    if (&rhs != this)
    {
        derivedInt = other.derivedInt;
        return *this;
    }
}

EDIT: Updated syntax and added CObject note 编辑:更新语法并添加CObject备注

In the general case, you do that either by explicitly calling operator= for the base class subobject, or by using the copy&swap idiom, if available: 在一般情况下,您可以通过显式调用operator =作为基类子对象,或者使用copy&swap惯用法(如果可用)来执行此操作:

//explicit call to base operator=
Derived& operator=(const Derived& rhs)
{
  Base::operator=(rhs); //
  derivedInt = rhs.derivedInt;
  return *this;
}


//or copy&swap:
Derived& operator=(const Derived& rhs)
{
  Derived tmp(rhs); //copy
  tmp.swap(*this);
  return *this;
}

void swap(Derived& other)   
{
  Base::swap(other);
  std::swap(derivedInt,other.derivedInt);
}

Update: since your base class is not meant to be copy-assigned, your derived class should not be copy-assigned either. 更新:由于您的基类不是要复制分配的,因此您的派生类也不应该被复制分配。 There are some cases where classes contain noncopyable parts but are perfectly copyable. 在某些情况下,类包含不可复制的部分,但完全可复制。 In those cases the noncopyable part is something that does not directly contribute to the class objects' state, eg a unique ID. 在这些情况下,不可复制的部分不会直接影响类对象的状态,例如唯一的ID。 Then those parts will normally not be changed during assignment. 然后在分配期间通常不会更改这些部件。 However, in those cases the noncopyable parts should not be contained by inheritance but rather by aggregation. 但是,在这些情况下,不可复制的部分不应该通过继承来包含,而应该通过聚合来包含。

Shortly said: Iheritance means "is-A" relationship. 不久说:Iheritance意味着“是-A”的关系。 Your Base cannot be copied. 您的基地无法复制。 Your Derived is a Base. 你的派生是一个基础。 Thus your Derived cannot be copied either. 因此,您的Derived也无法复制。

Like this 像这样

Derived& operator=(const Derived& rhs)
{
    Base::operator=(rhs);
    derivedInt = rhs.derivedInt;
    return *this;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM