C++包装模板化函数

发布于 2024-10-20 03:02:17 字数 1252 浏览 2 评论 0 原文

  • 我有一个模板类 A,以及 A 的流运算符函数,因此它是一个模板函数。
  • 我有一个继承自 A 的类 B,指定模板的类型并添加一些特定于 B 的内容。
  • 我正在 B 上编写一个流运算符,它执行特定于 B 的操作,然后回退到编程的其余流操作因为

B 是 A 的子类(你这么称呼它吗?),所以调用 A 的流运算符应该可以工作。事实上,main() 中的operator>>(f,a) 是有效的。但由于某种原因,它不适用于 b 转换为 A。我得到的错误是“没有匹配的函数来调用 'operator>>(std::basic_istream >&, A)”。

我做错了什么?

这是示例代码:

#include <stdlib.h>
#include <fstream>
#include <iostream>

using namespace std;


template <class TypeT>
class A
{
public:
    A(){a = -9;}
    A(TypeT v){a = v;}
    TypeT a;
};

class B : public A<int>
{
public:
    B(int w) : A<int>(10) {b = w;}
    int b;
};

template <class TypeT>
istream &operator>> (istream &s, A<TypeT> &a)
{
    cout << "a.a = " << a.a << endl;
    return s;
}

istream &operator>> (istream &s, B &b)
{
    cout << "b.b = " << b.b << "  ";
    operator>>( s, (A<int>)b);    // error!
    return s;
}



int main(void) {
    ifstream f("/dev/null");

    A<int> a(0);
    operator>>( f, a );

    B b(1);
    operator>>( f, b );

    return EXIT_SUCCESS;
}

  • I have a template class A, and a stream operator function for A that is consequently a template function.
  • I have a class B that inherit from A, specifying a type for the template and adding some stuff specific to B.
  • I am writing a stream operator on B that does something specific to B and then falls back to the rest of the stream operations programmed for A.

Because B is a subclass of A (do you call it that?), invoking A's stream operator should work. In fact, operator>>(f,a) in main() works. But for some reason it doesn't work on b casted to A. The error I get is "no matching function for call to ‘operator>>(std::basic_istream >&, A)".

What am I doing wrong?

Here is the sample code:

#include <stdlib.h>
#include <fstream>
#include <iostream>

using namespace std;


template <class TypeT>
class A
{
public:
    A(){a = -9;}
    A(TypeT v){a = v;}
    TypeT a;
};

class B : public A<int>
{
public:
    B(int w) : A<int>(10) {b = w;}
    int b;
};

template <class TypeT>
istream &operator>> (istream &s, A<TypeT> &a)
{
    cout << "a.a = " << a.a << endl;
    return s;
}

istream &operator>> (istream &s, B &b)
{
    cout << "b.b = " << b.b << "  ";
    operator>>( s, (A<int>)b);    // error!
    return s;
}



int main(void) {
    ifstream f("/dev/null");

    A<int> a(0);
    operator>>( f, a );

    B b(1);
    operator>>( f, b );

    return EXIT_SUCCESS;
}

.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

金兰素衣 2024-10-27 03:02:17

将您的强制转换更改为:

operator>>(s, (A<int> &)b);

您的原始强制转换创建一个临时变量,您只能获得一个 const 引用。

Change your cast to:

operator>>(s, (A<int> &)b);

Your original cast creates a temporary which you can only get a const reference to.

不必了 2024-10-27 03:02:17

问题是您的 C-cast 显然是在创建一个新对象,而不是按预期向上转换。由于这个新对象是临时对象,因此无法将其绑定到父级运算符函数的非常量引用参数。如果您 static_cast 到对父级的引用,它应该可以工作:

operator>>( s, static_cast&>(b));

The problem is that your C-cast is apparently creating a new object rather than upcasting as expected. Since this new object is a temporary it can't be bound to the non-const reference parameter to the parent's operator function. If you static_cast to a reference to the parent it should work:

operator>>( s, static_cast<A<int>&>(b));

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