【发布时间】:2011-08-17 21:31:47
【问题描述】:
所以我用这样的Log class:
#include <stdio.h>
#include <iostream>
class Log
{
public:
int i;
Log()
{
i = 0;
}
template <class T>
Log &operator<<(const T &v)
{
i++;
std::cout << i << ":" << v << ";" <<std::endl;
return *this;
}
Log &operator<<(std::ostream&(*f)(std::ostream&))
{
i++;
std::cout << i << ":" << *f << ";" <<std::endl;
return *this;
}
~Log()
{
std::cout << " [end of message]" << std::endl;
}
};
我使用的是这样的:
#include <log.h>
int main()
{
Log a;
a << "here's a message" << std::endl;
a << "here's one with a number: " << 5;
std::cin.get();
}
我希望我的日志类在我输入“;”时得到意思是如果我有 a << "here's a message" << std::endl; 我希望它能够得到它是一个日志消息,而 a << "here's one with a number: " << 5; 是另一个。
它会输出下一条消息:
1:here's a message;
2:
;
3:here's one with a number: ;
4:5;
我想保留它的 sintax(无限数量的 <<,大量的值类型,在 api 中没有 ( 和 ))但让它输出:
1:here's a message
;
2:here's one with a number: 5;
这样的事情怎么办?
【问题讨论】:
标签: c++ templates logging stl std