21
|
David Rodríguez - dribeas · 技术社区 · 15 年前 |
1
14
事情是这样的: 如果我将[1]更改为
然后事情按照你期望的方式进行。中有一个自动生成的赋值运算符
在您的示例中,编译器有足够的信息来猜测这一点
|
2
5
在这种情况下,有三个运算符=:
这解释了为什么在案例[1]中,Base::operator=(Base const&)被称为“虚拟的”。它是从编译器生成的版本调用的。这同样适用于案例[3]。在案例2中,右侧参数“bb”的类型为Base&,因此无法调用派生::运算符=(派生&)。 |
3
4
没有为派生类定义用户提供的赋值运算符。所以,编译器合成一个基类赋值运算符,并从该派生类的合成赋值运算符调用基类赋值运算符。
因此
在派生类中,基类赋值运算符已被重写,因此,重写的方法在派生类的虚拟表中获取一个条目。当通过引用或指针调用该方法时,由于运行时的VTable条目解析,将调用派生类重写的方法。
==>内部==>(对象->VTable[赋值运算符]) |
4
3
如果您未能提供适当的
检查这个 link |
5
2
原因是编译器提供了默认赋值
有关虚拟分配的更多说明,请访问: https://stackoverflow.com/a/26906275/3235055 |