- 论坛徽章:
- 0
|
原帖由 converse 于 2006-4-19 23:21 发表
关于operator = 的问题,我记不得在哪里看过说把这个函数声明成virtual的时候需要注意一些什么了,改天我头脑清醒一点的时候再找找,昨晚熬夜看球精神有点恍惚了:)
operator= is not inherited. You can verify this by attempting to inherit an operator= some time. It definitely does not work. This might cause you to believe that declaring them to be virtual is meaningless, but this is not the case.
If class A has:
virtual A& operator=(const A&);
and Class B derived from A has
virtual A& operator=(const A&);
Then the following code will invoke B's operator=:
A a;
B b;
A &r = b;
r=a; // virtually invokes B's operator=
So, you would want to make operator= any time you thought you might be doing polymorphic assigment through a reference....
Note however that both assignment operators have to have the same signature. The utility of A& B::operator=(const A&) is unclear, and probably not significant. The important thing to remember is that
B& B::operator=(const B&);
Does not override, nor virtually substitute for
A& A::operator=(const A&);
So, usually, there is not much point in making operator= virtual.
我帮你把表情禁用了,这样方便查看--converse
[ 本帖最后由 converse 于 2006-4-21 12:58 编辑 ] |
|