C++:继承和运算符重载
我有两个结构:
template <typename T>
struct Odp
{
T m_t;
T operator=(const T rhs)
{
return m_t = rhs;
}
};
struct Ftw : public Odp<int>
{
bool operator==(const Ftw& rhs)
{
return m_t == rhs.m_t;
}
};
我想编译以下内容:
int main()
{
Odp<int> odp;
odp = 2;
Ftw f;
f = 2; // C2679: no operator could be found
}
有什么方法可以使其工作,或者我也必须在 Ftw
中定义运算符吗?
I have two structs:
template <typename T>
struct Odp
{
T m_t;
T operator=(const T rhs)
{
return m_t = rhs;
}
};
struct Ftw : public Odp<int>
{
bool operator==(const Ftw& rhs)
{
return m_t == rhs.m_t;
}
};
I would like the following to compile:
int main()
{
Odp<int> odp;
odp = 2;
Ftw f;
f = 2; // C2679: no operator could be found
}
Is there any way to make this work, or must I define the operator in Ftw
as well?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是编译器通常会为您创建一个operator=(除非您提供),而这个operator=隐藏了继承的operator=。您可以通过使用声明来推翻这一点:
The problem is that the compiler usually creates an
operator=
for you (unless you provide one), and thisoperator=
hides the inherited one. You can overrule this by using-declaration: