【发布时间】:2013-12-04 16:30:58
【问题描述】:
我目前正在尝试过滤流入“阻塞”流缓冲区的输入。因此,我想创建一个类“MyClog”,它重载它的
过滤取决于 MyClog 类的内部状态。另外我想使用模板重载运算符,所以我只需要实现一种方法。
例子
clog << "This is a Test No." << 1;
myclog << "This is a Test No." << 2
根据 myclog 的内部状态,消息通过 clog 打印出来。
如果我不使用模板,我的重载运算符可以工作,我做错了什么? template<typename T>和template<class T>有什么区别?
这是我的代码的摘录。
template<typename T>
class MyClog {
private:
bool state=false;
public:
MyClog& operator<<(const T& arg){
if(state){
clog << arg;
}
return *this;
}
【问题讨论】:
标签: c++ templates filter overloading operator-keyword