首页 > 解决方案 > 类模板是否可以接收类以打印其成员?

问题描述

我通常operator<<A课堂上写,但我想知道我是否可以制作一个B可以为我做这件事的模板类。

class A {
    long a;
    string b;
    int c;

public:
    A(long a, string b) : a(a), b(move(b)), c(c) {}
};
template<typename T>
class B {
    T foo;

public:
    // this should print all the members of T
    friend ostream& operator<<(ostream& os, const T& obj);
};

标签: c++templates

解决方案


推荐阅读