虚拟赋值运算符C ++

可以将C ++中的Assignment Operator虚拟化。为什么需要它?我们也可以使其他运营商虚拟吗?



BIG阳
浏览 621回答 3
3回答

GCT1015

不需要将赋值运算符设为虚拟。下面的讨论是关于的operator=,但它也适用于接受了所讨论类型的任何运算符重载,以及接受了所讨论类型的任何函数。下面的讨论表明,在寻找匹配的函数签名方面,虚拟关键字不知道参数的继承。在最后一个示例中,它显示了在处理继承类型时如何正确处理分配。虚函数不知道参数的继承:要使虚拟功能发挥作用,功能的签名必须相同。因此,即使在以下示例中,将operator =设为虚拟,该调用也永远不会充当D中的虚拟函数,因为operator =的参数和返回值不同。该功能B::operator=(const B& right)与D::operator=(const D& right)100%完全不同,被视为2个不同的功能。class B{public:&nbsp; virtual B& operator=(const B& right)&nbsp; {&nbsp; &nbsp; x = right.x;&nbsp; &nbsp; return *this;&nbsp; }&nbsp; int x;};class D : public B{public:&nbsp; virtual D& operator=(const D& right)&nbsp; {&nbsp; &nbsp; x = right.x;&nbsp; &nbsp; y = right.y;&nbsp; &nbsp; return *this;&nbsp; }&nbsp; int y;};默认值,并具有2个重载运算符:尽管可以定义一个虚函数,以便在将D分配给类型B的变量时为D设置默认值。即使您的B变量确实是存储在B的引用中的D,也不会。D::operator=(const D& right)功能。在以下情况下,将使用存储在2个B引用中的2个D对象的赋值...使用D::operator=(const B& right)覆盖。//Use same B as aboveclass D : public B{public:&nbsp; virtual D& operator=(const D& right)&nbsp; {&nbsp; &nbsp; x = right.x;&nbsp; &nbsp; y = right.y;&nbsp; &nbsp; return *this;&nbsp; }&nbsp; virtual B& operator=(const B& right)&nbsp; {&nbsp; &nbsp; x = right.x;&nbsp; &nbsp; y = 13;//Default value&nbsp; &nbsp; return *this;&nbsp; }&nbsp; int y;};int main(int argc, char **argv)&nbsp;{&nbsp; D d1;&nbsp; B &b1 = d1;&nbsp; d1.x = 99;&nbsp; d1.y = 100;&nbsp; printf("d1.x d1.y %i %i\n", d1.x, d1.y);&nbsp; D d2;&nbsp; B &b2 = d2;&nbsp; b2 = b1;&nbsp; printf("d2.x d2.y %i %i\n", d2.x, d2.y);&nbsp; return 0;}印刷品:d1.x d1.y 99 100d2.x d2.y 99 13这表明D::operator=(const D& right)从未使用过。如果没有启用virtual关键字,B::operator=(const B& right)您将获得与上述相同的结果,但是y的值将不会初始化。即它将使用B::operator=(const B& right)RTTI将这一切捆绑在一起的最后一步:您可以使用RTTI正确处理传入类型的虚函数。这是弄清楚如何处理可能继承的类型时如何正确处理分配的难题的最后一部分。virtual B& operator=(const B& right){&nbsp; const D *pD = dynamic_cast<const D*>(&right);&nbsp; if(pD)&nbsp; {&nbsp; &nbsp; x = pD->x;&nbsp; &nbsp; y = pD->y;&nbsp; }&nbsp; else&nbsp; {&nbsp; &nbsp; x = right.x;&nbsp; &nbsp; y = 13;//default value&nbsp; }&nbsp; return *this;}

繁花如伊

RTTI将这一切捆绑在一起的最后一步:您可以使用RTTI正确处理传入类型的虚函数。这是弄清楚如何处理可能继承的类型时如何正确处理分配的难题的最后一部分。virtual B& operator=(const B& right){&nbsp; const D *pD = dynamic_cast<const D*>(&right);&nbsp; if(pD)&nbsp; {&nbsp; &nbsp; x = pD->x;&nbsp; &nbsp; y = pD->y;&nbsp; }&nbsp; else&nbsp; {&nbsp; &nbsp; x = right.x;&nbsp; &nbsp; y = 13;//default value&nbsp; }&nbsp; return *this;}我想在此解决方案中添加一些说明。让赋值运算符声明与上述相同有三个问题。编译器生成一个赋值运算符,该赋值运算符带有一个const D&参数,该参数不是虚拟的,并且没有执行您可能认为的操作。第二个问题是返回类型,您正在返回对派生实例的基本引用。无论如何,代码可能不会有太大的问题。还是最好相应地返回引用。第三个问题,派生类型赋值运算符不调用基类赋值运算符(如果要复制私有字段,该怎么办?),将赋值运算符声明为virtual不会使编译器为您生成一个。这是没有赋值运算符的至少两个重载来获得所需结果的副作用。考虑基类(与我引用的帖子中的基类相同):class B{public:&nbsp; &nbsp; virtual B& operator=(const B& right)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; x = right.x;&nbsp; &nbsp; &nbsp; &nbsp; return *this;&nbsp; &nbsp; }&nbsp; &nbsp; int x;};以下代码完善了我引用的RTTI解决方案:class D : public B{public:&nbsp; &nbsp; // The virtual keyword is optional here because this&nbsp; &nbsp; // method has already been declared virtual in B class&nbsp; &nbsp; /* virtual */ const D& operator =(const B& b){&nbsp; &nbsp; &nbsp; &nbsp; // Copy fields for base class&nbsp; &nbsp; &nbsp; &nbsp; B::operator =(b);&nbsp; &nbsp; &nbsp; &nbsp; try{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const D& d = dynamic_cast<const D&>(b);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Copy D fields&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y = d.y;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch (std::bad_cast){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Set default values or do nothing&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return *this;&nbsp; &nbsp; }&nbsp; &nbsp; // Overload the assignment operator&nbsp; &nbsp; // It is required to have the virtual keyword because&nbsp; &nbsp; // you are defining a new method. Even if other methods&nbsp; &nbsp; // with the same name are declared virtual it doesn't&nbsp; &nbsp; // make this one virtual.&nbsp; &nbsp; virtual const D& operator =(const D& d){&nbsp; &nbsp; &nbsp; &nbsp; // Copy fields from B&nbsp; &nbsp; &nbsp; &nbsp; B::operator =(d);&nbsp; &nbsp; &nbsp; &nbsp; // Copy D fields&nbsp; &nbsp; &nbsp; &nbsp; y = d.y;&nbsp; &nbsp; &nbsp; &nbsp; return *this;&nbsp; &nbsp; }&nbsp; &nbsp; int y;};这似乎是一个完整的解决方案,但事实并非如此。这不是一个完整的解决方案,因为当您从D派生时,您将需要1个运算符=接受const B&,1个运算符=接受const D&,以及一个需要const D2&的运营商。结论是显而易见的,运算符=()重载的数量等于超类的数量+ 1。考虑到D2继承了D,让我们看一下两个继承的operator =()方法的外观。class D2 : public D{&nbsp; &nbsp; /* virtual */ const D2& operator =(const B& b){&nbsp; &nbsp; &nbsp; &nbsp; D::operator =(b); // Maybe it's a D instance referenced by a B reference.&nbsp; &nbsp; &nbsp; &nbsp; try{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const D2& d2 = dynamic_cast<const D2&>(b);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Copy D2 stuff&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch (std::bad_cast){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Set defaults or do nothing&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return *this;&nbsp; &nbsp; }&nbsp; &nbsp; /* virtual */ const D2& operator =(const D& d){&nbsp; &nbsp; &nbsp; &nbsp; D::operator =(d);&nbsp; &nbsp; &nbsp; &nbsp; try{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const D2& d2 = dynamic_cast<const D2&>(d);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Copy D2 stuff&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch (std::bad_cast){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Set defaults or do nothing&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return *this;&nbsp; &nbsp; }};显然,运算符=(const D2&)仅复制字段,想象一下它在那里。我们可以注意到继承的运算符=()重载中的模式。遗憾的是,我们无法定义可以处理这种模式的虚拟模板方法,我们需要多次复制和粘贴相同的代码才能获得完整的多态赋值运算符,这是我所看到的唯一解决方案。也适用于其他二进制运算符。编辑如评论中所述,使生活变得更轻松的最少方法是定义最顶层的超类赋值运算符=(),然后从所有其他超类运算符=()方法中调用它。同样,在复制字段时,可以定义_copy方法。class B{public:&nbsp; &nbsp; // _copy() not required for base class&nbsp; &nbsp; virtual const B& operator =(const B& b){&nbsp; &nbsp; &nbsp; &nbsp; x = b.x;&nbsp; &nbsp; &nbsp; &nbsp; return *this;&nbsp; &nbsp; }&nbsp; &nbsp; int x;};// Copy method usageclass D1 : public B{private:&nbsp; &nbsp; void _copy(const D1& d1){&nbsp; &nbsp; &nbsp; &nbsp; y = d1.y;&nbsp; &nbsp; }public:&nbsp; &nbsp; /* virtual */ const D1& operator =(const B& b){&nbsp; &nbsp; &nbsp; &nbsp; B::operator =(b);&nbsp; &nbsp; &nbsp; &nbsp; try{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _copy(dynamic_cast<const D1&>(b));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch (std::bad_cast){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Set defaults or do nothing.&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return *this;&nbsp; &nbsp; }&nbsp; &nbsp; virtual const D1& operator =(const D1& d1){&nbsp; &nbsp; &nbsp; &nbsp; B::operator =(d1);&nbsp; &nbsp; &nbsp; &nbsp; _copy(d1);&nbsp; &nbsp; &nbsp; &nbsp; return *this;&nbsp; &nbsp; }&nbsp; &nbsp; int y;};class D2 : public D1{private:&nbsp; &nbsp; void _copy(const D2& d2){&nbsp; &nbsp; &nbsp; &nbsp; z = d2.z;&nbsp; &nbsp; }public:&nbsp; &nbsp; // Top-most superclass operator = definition&nbsp; &nbsp; /* virtual */ const D2& operator =(const B& b){&nbsp; &nbsp; &nbsp; &nbsp; D1::operator =(b);&nbsp; &nbsp; &nbsp; &nbsp; try{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _copy(dynamic_cast<const D2&>(b));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch (std::bad_cast){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Set defaults or do nothing&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return *this;&nbsp; &nbsp; }&nbsp; &nbsp; // Same body for other superclass arguments&nbsp; &nbsp; /* virtual */ const D2& operator =(const D1& d1){&nbsp; &nbsp; &nbsp; &nbsp; // Conversion to superclass reference&nbsp; &nbsp; &nbsp; &nbsp; // should not throw exception.&nbsp; &nbsp; &nbsp; &nbsp; // Call base operator() overload.&nbsp; &nbsp; &nbsp; &nbsp; return D2::operator =(dynamic_cast<const B&>(d1));&nbsp; &nbsp; }&nbsp; &nbsp; // The current class operator =()&nbsp; &nbsp; virtual const D2& operator =(const D2& d2){&nbsp; &nbsp; &nbsp; &nbsp; D1::operator =(d2);&nbsp; &nbsp; &nbsp; &nbsp; _copy(d2);&nbsp; &nbsp; &nbsp; &nbsp; return *this;&nbsp; &nbsp; }&nbsp; &nbsp; int z;};不需要设置默认值的方法,因为它只会收到一个调用(在基本运算符=()重载中)。在一个地方完成复制字段时的更改,并且所有operator =()重载都将受到影响并具有其预期的目的。
打开App,查看更多内容
随时随地看视频慕课网APP