【发布时间】:2018-11-30 23:16:32
【问题描述】:
我有一个关于接受父引用类型的operator= 的问题。
当有一个抽象类并且它是实现类时,
为什么只有一个接受父引用类型的operator= 还不够?
下面是我的代码
#include <iostream>
class AbstractType {
public:
virtual ~AbstractType() {}
virtual AbstractType& operator=(const AbstractType& other) {
std::cout << "AbstractType& operator=(const AbstractType&)" << std::endl;
return *this;
}
};
class Type1: public AbstractType {
public:
virtual ~Type1() {}
virtual Type1& operator=(const AbstractType& other) {
std::cout << "Type1& operator=(const AbstractType&)" <<
std::endl;
return *this;
}
/*
virtual Type1& operator=(const Type1& other) {
std::cout << "Type1& operator=(const Type1&)" << std::endl;
// Just redirecting here! What a waste!
return operator=(dynamic_cast<const AbstractType&>(other));
}
*/
};
int main()
{
Type1 t1;
AbstractType& r1 = t1;
Type1 t2;
AbstractType& r2 = t2;
// Type1& operator=(const AbstractType&). It's fine!
r1 = r2;
// AbstractType& operator=(const AbstractType&)!! Why??
// Expected `Type1& operator=(const AbstractType&)` to be called!
t1 = t2;
return 0;
}
您可以发现给定的参数只是在Type1& operator=(const Type1&) 中被重定向,而被注释忽略。
取消注释Type1& operator=(const Type1&) 只是为我工作,
但是如果说,我有一百多个 TypeX,那么我必须进行两百个复制分配,这是我无法理解的,因为在我看来,只有 Type1& operator=(const AbstractType& other) 就足够了。
在大多数情况下,我只有 AbstractType 来处理周围的事情。 在有限的情况下,很少能提前知道它的具体类型。
谁能建议我更好的解决方案?
【问题讨论】:
-
由于您无法更改现有对象的类型,因此将赋值运算符设为虚拟没有多大意义。您增加了在代码中引入错误的机会。也许您想克隆对象并动态创建它们。
-
多态类型的赋值很少有意义。也许您需要操纵(智能)指向多态对象的指针,而不是对象本身。
-
@Phil1970, @n.m.感谢你的建议!但我有两个 AbstractionType 列表。当访问每个列表中的相同索引时,将确保相同的类型,因此它们具有相同的大小。我想做的是在循环期间进行分配。在伪代码中,它将是
for i in 0 to length; *(dest[i]) = *(source[i])。这就是制作虚拟算子的原因。有什么建议吗?谢谢! -
@0ctopus13prime 如果你做一个循环,然后显示一些代码来显示你的数组是如何声明的。我们已经告诉过您,在大多数情况下,虚拟作业没有任何意义(对于了解该语言的人),但在每个回答者中,您似乎并不真正理解任何给定的评论。 您的问题遗漏了有助于我们清楚了解您想要实现的目标的详细信息! 可能有很多方法可以解决您的问题而虚拟作业可能不是其中之一。 停止试图为工作使用错误的工具。如果你想复制一个数组,克隆就是答案。
标签: c++ operator-keyword