当调用基类的指针时,赋值运算符不会重载吗?
我遇到了以下问题,这向我证明我对 C++ 的工作原理知之甚少。
我使用带有纯虚函数的基类
class Base
...
和类型的派生类,
class Derived : public Base{
private:
Foo* f1;
...
两者都实现了赋值运算符。除此之外,Derived 的赋值运算符复制 f1 中的数据。在我的代码中,我创建了 Derived 类的两个新实例
Base* d1 = new Derived();
Base* d2 = new Derived();
如果我现在调用赋值运算符,
*d1 = *d2;
则不会调用 Derived 的赋值运算符,并且不会复制 f1 中的数据!只有我这样做才有效
*dynamic_cast<Derived*>(d1) = *dynamic_cast<Derived*>(d2);
有人可以解释为什么赋值运算符没有重载吗?
谢谢!
I have encountered the following problem which proved to me that I know far too little about the workings of C++.
I use a base class with pure virtual functions
class Base
...
and a derived classes of type
class Derived : public Base{
private:
Foo* f1;
...
Both have assignment operators implemented. Among other things, the assignment operator for Derived copies the data in f1. In my code, I create two new instances of class Derived
Base* d1 = new Derived();
Base* d2 = new Derived();
If I now call the assignment operator
*d1 = *d2;
the assignment operator of Derived is not called, and the data in f1 is not copied! It only works if I do
*dynamic_cast<Derived*>(d1) = *dynamic_cast<Derived*>(d2);
Can someone explain why the assignment operators are not overloaded?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
没有看到相关代码就很难说。这是一个有效的示例:
运行时将打印
B
。您可能做错的事情:
operator=
设为虚拟。operator=
作为签名,该签名比父类的operator=
的签名更具限制性,因此您实际上并未覆盖父类的定义。例如,如果您更改B&运算符=(A&a) { cout << “B”<<结束; }
至B&运算符=(B&a) { cout << “B”<<结束; }
在上面的例子中,它将不再打印B
。It's hard to say without seeing the relevant code. Here's an example that works:
This will print
B
when run.Things that you might be doing wrong:
operator=
virtual in the base class.operator=
as signature that's more restrictive than that of the parent'soperator=
, so you're not actually overriding the parent's definition. For example if you changeB& operator=(A& a) { cout << "B" << endl; }
toB& operator=(B& a) { cout << "B" << endl; }
in the example above, it will no longer printB
.我对已接受的答案有一些不同的看法。这基本上是
更有效的 C++
第 33 项。因此,即使公认的解决方案有效,我认为重要的是要指出使赋值运算符虚拟化所涉及的危险!I have some alternate perspective on the accepted answer. This is basically
More Effetive C++
Item 33. So even though the accepted solution works, I think it is important to bring out the dangers involved in making assignment operator virtual!为了使虚拟工作正常,您需要相同的签名。因此,如果您的
operator=
在Derived
类中使用的const Derived&
参数与在Derived
类中使用的参数不匹配code>const Base& 基类中的参数。这意味着您可以实现多态性,但您需要在派生类中有一个
operator=(const Base&)
才能实现这一点。For virtual to work you need the same signature. So if your
operator=
works on aconst Derived&
parameter in yourDerived
class that doesn't match the one that is working on aconst Base&
parameter in your base class.This means that you can accomplish polymorphism but you need to have an
operator=(const Base&)
in your derived class to do so.