c++ - 类模板是否可以接收类以打印其成员?
问题描述
我通常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);
};
解决方案
推荐阅读
- php - 在每个 foreach 中使用多个 foreach 插入仅在每个 foreach 中获取最终值
- regex - 用于在 MATLAB 中捕获科学数字的 RegEx
- c# - 如何将新数据成员添加到我的自定义复选框列表?
- sql-server - 如何让自动迁移与 SQL Server 一起工作
- javascript - 我如何显示我的登录用户下的他/她的订单列表?
- javascript - 如何修复 vue js 或 javascript 比较逻辑错误
- node.js - 我根本无法启动管理 SDK
- javascript - 如何将使用 id 的函数替换为类名
- android - react-native - 如何检测何时按下主页按钮?
- image - React Native Animated.event onScroll 从左上角而不是中心缩放图像