我可以覆盖运算符的重载并返回不同的类型吗?
class A{
public:
virtual char &operator[](int);
protected:
..
};
class B:A{
public:
A* &operator[](int);
protected:
}
当我重载运算符的重载时,我可以更改返回类型吗?
谢谢!
//编辑 好的,既然我们已经确定这行不通,我该如何解决呢?
假设我有 A、B、C 和 D 类。
class A{
public:
private:
char &operator[](int);
protected:
..
};
class B:A{
public:
virtual char &operator[](int);
};
class C: A{
public:
private:
A::&operator[](int);
}
class D: A{
public:
private:
A::&operator[](int);
}
我可以做这样的事情吗?如果是这样,这是正确的语法吗?
class A{
public:
virtual char &operator[](int);
protected:
..
};
class B:A{
public:
A* &operator[](int);
protected:
}
Can I change the return type when I overload an overload of an operator?
thanks!
//EDIT
Okay, so now that we established that this wont work how can I build a work around?
Lets say I have classes A,B,C, and D.
class A{
public:
private:
char &operator[](int);
protected:
..
};
class B:A{
public:
virtual char &operator[](int);
};
class C: A{
public:
private:
A::&operator[](int);
}
class D: A{
public:
private:
A::&operator[](int);
}
Can I do something like this? If so is this the correct syntax?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不是这样的,不。
重写的返回类型必须与
因此,如果虚拟
A::operator[]
返回A*
,则B::operator[]
重写可能会返回 <代码>B*。Not like this, no.
The return type of an override must be either
So, if a virtual
A::operator[]
returned anA*
, then aB::operator[]
override could return aB*
.多态函数不能在不同类中返回不同类型的原因并不是因为 C++ 委员会中的某个人认为这是“禁忌”,而是因为任何使用该函数返回值的代码都无法编译。
通过创建继承层次结构,您可以通过基指针或引用访问派生对象:
请注意,在最后一行,编译器不会知道
a
指向什么类型的对象,因为这是在运行时确定的。这很好,因为无论a
指向什么类型的对象,operator[]
仍然会返回一个字符。但是,如果允许该运算符在B
类中返回不同类型怎么办?显然,当
a
是B
类型的对象时,最后一行没有任何意义。在这种情况下,您尝试将序列
分配给角色。同样,如果a
是A
类型的对象,则Sequence c = (*a)[0];
就没有意义。The reason that a polymorphic function can't return different types in different classes isn't because someone on the C++ committee decided that it was "taboo", but because any code that used that function's return value couldn't compile.
By creating an inheritance heirarchy, you're able to access derived objects through a base pointer or reference:
Note that on the last line, the compiler won't know what type of object
a
is pointing to, since that's determined at runtime. This is fine, because no matter what type of objecta
is pointing to,operator[]
is still going to return a character. But what if that operator were allowed to return a different type in classB
?Obviously, that last line makes no sense when
a
is an object of typeB
. In that case, you're trying to assign aSequence
to a character. Likewise,Sequence c = (*a)[0];
wouldn't make sense ifa
were an object of typeA
.正确的解决方案是使
A
成为类C
和D
的成员,而不是基类。由于C
和D
需要为公共方法使用不同的签名,因此它们显然不等于每个上下文中的A
,因此尝试在这里使用继承确实没有意义。The proper solution is to make
A
a member of classesC
andD
rather than a base class. SinceC
andD
need a different signature for a public method, they're clearly not equivalent to anA
in every context, so trying to use inheritance here really makes no sense.好的,看看您关于序列 (
A
)、GeneSequence (B
) 和 GenomeSequence (C
) 的最后评论,您希望操作员有时返回一个
char
,有时会返回一个Sequence
那么,如您所见,超类
A
声明的类型是不正确的>&运算符 []
作为char
。它应该使用 template 作为类型参数,以指定它是
char
的集合还是Sequence
的集合,对吧?OK looking at your last comment about Sequence (
A
), GeneSequence (B
) and GenomeSequence (C
)you want the operator to sometimes return a
char
, and sometimes return aSequence
well then, as you can see, it is incorrect for the superclass
A
to declare the type of&operator []
aschar
.It should use a template as a type parameter, to specify whether it is a collection of
char
s orSequence
s, right?