C++模板类继承和运算符使用

发布于 2024-12-14 04:39:27 字数 895 浏览 2 评论 0原文

我有一个定义运算符的模板类,它适用于模板参数。 我有另一个类继承自这个类,我当然希望操作符被继承。

考虑一下:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

辞旧 2024-12-21 04:39:27

问题不在于调用运算符,而在于从返回值(即 A)构造 B

如果 B 不包含其基类 A 之外的任何数据,则可以从 A 为 B 提供构造函数

template <typename T>
class B : public A<T>
{
public:
     B(const T& x) : A<T>(x) {}
     B(const A<T>&x) : A<T>(x) {}
  // additional stuff here
};

如果B确实包含自己的数据,那么您肯定需要实现operator-来处理这些数据字段吗?

The issue is not with calling the operator, but constructing a B from the return value which is an A.

If B does not contain any data other than in its base class A, you can provide a constructor for a B from an A:

template <typename T>
class B : public A<T>
{
public:
     B(const T& x) : A<T>(x) {}
     B(const A<T>&x) : A<T>(x) {}
  // additional stuff here
};

If B does contain its own data, then surely you need to implement the operator- to handle those data fields?

淡写薰衣草的香 2024-12-21 04:39:27

当我尝试此操作时,Visual Studio 给出错误 cannot conversion from 'A'到 'B'

所以我尝试将变量 f 的类型从 B 更改为 A code> 并正确编译。这就是我所拥有的:

int main()
{
  // Fine
  A<int> a(5);
  A<int> b(2);
  A<int> c = a - b;
  std::cout << c.x() << std::endl;

  // does compile :)
  B<int> d(5);
  B<int> e(2);
  A<int> f = d - e;
  std::cout << f.x() << std::endl;

  return 0;
}

但是,我认为这不是您想要的,因为如果您找到一种将 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 from B<int> to A<int> and it compiled properly. Here's what I have:

int main()
{
  // Fine
  A<int> a(5);
  A<int> b(2);
  A<int> c = a - b;
  std::cout << c.x() << std::endl;

  // does compile :)
  B<int> d(5);
  B<int> e(2);
  A<int> f = d - e;
  std::cout << f.x() << std::endl;

  return 0;
}

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 exactly sizeof(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文