C++模板类继承和运算符使用
我有一个定义运算符的模板类,它适用于模板参数。 我有另一个类继承自这个类,我当然希望操作符被继承。
考虑一下:
template <typename T>
class A
{
public:
A(const T& x) : x_(x) {}
A operator-(const A& other)
{
A r(*this);
r.x_ -= other.x_;
return r;
}
T x() const { return x_; }
private:
T x_;
};
template <typename T>
class B : public A<T>
{
// additional stuff here
};
我似乎无法对 B 类型的对象使用 A 中声明的任何运算符。
示例:
int main()
{
// Fine
A<int> a(5);
A<int> b(2);
A<int> c = a - b;
std::cout << c.x() << std::endl;
// Won't compile :(
B<int> d(5);
B<int> e(2);
B<int> f = d - e;
std::cout << f.x() << std::endl;
return 0;
}
将触发以下错误:错误:请求从“A”转换为非标量类型“B”< /em>
有什么办法可以做到这一点吗?我真的想避免在 B 类中重写所有代码(完全相同)。
谢谢!
I have a template class which defines operators, that works on the template parameters.
I have another class that inherits from this class, and I want the operators to be inherited of course.
Consider this:
template <typename T>
class A
{
public:
A(const T& x) : x_(x) {}
A operator-(const A& other)
{
A r(*this);
r.x_ -= other.x_;
return r;
}
T x() const { return x_; }
private:
T x_;
};
template <typename T>
class B : public A<T>
{
// additional stuff here
};
I cannot seem to use any of the operators declared in A for the objects of type B.
Example:
int main()
{
// Fine
A<int> a(5);
A<int> b(2);
A<int> c = a - b;
std::cout << c.x() << std::endl;
// Won't compile :(
B<int> d(5);
B<int> e(2);
B<int> f = d - e;
std::cout << f.x() << std::endl;
return 0;
}
Will trigger the following error: error: conversion from ‘A’ to non-scalar type ‘B’ requested
Is there any way this can work? I really want to avoid re-writing all the code (which would be exactly the same) in class B.
Thanks !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题不在于调用运算符,而在于从返回值(即
A
)构造B
。如果
B
不包含其基类A
之外的任何数据,则可以从A 为
:B
提供构造函数如果
B
确实包含自己的数据,那么您肯定需要实现operator-
来处理这些数据字段吗?The issue is not with calling the operator, but constructing a
B
from the return value which is anA
.If
B
does not contain any data other than in its base classA
, you can provide a constructor for aB
from anA
:If
B
does contain its own data, then surely you need to implement theoperator-
to handle those data fields?当我尝试此操作时,Visual Studio 给出错误
cannot conversion from 'A'到 'B'
所以我尝试将变量
f
的类型从B
更改为A
code> 并正确编译。这就是我所拥有的:但是,我认为这不是您想要的,因为如果您找到一种将 f 从 B 转换为 A 的方法,您会发现特定于 B 的所有数据成员都已被删除。如果您考虑一下处理器在幕后的作用,就会发现这是有道理的。当它看到
operator-
的返回类型是 A 时,它正好返回sizeof(A)
字节的数据。除此之外的任何额外数据(例如与 B 相关的数据)都将被删除。因此,为了安全起见,如果您尝试将返回值放入 B 类型的变量,编译器会告诉您,您运气不好。When I try this, visual studio gives me the error
cannot convert from 'A<T>' to 'B<T>'
So I tried changing the type of the variable
f
fromB<int>
toA<int>
and it compiled properly. Here's what I have:However, I don't think this is what you want, because if you found a way to cast f from B to A you would find that all of the data members specific to B had been erased. If you think about what the processor is doing under the hood, this makes sense. When it sees that the return type of
operator-
is A, it returns exactlysizeof(A)
bytes of data. Any extra data on top of that (such as the data associated with B) is chopped off. So to be safe the compiler tells you that you're out of luck if you try to put the return value into a variable of type B.