【发布时间】:2014-10-20 03:26:58
【问题描述】:
我对 C++ 比较陌生。如果不正确,请原谅我的术语。我试图四处寻找我的问题的答案,但找不到(可能是因为我无法正确表达我的问题)。如果有人可以帮助我,我将不胜感激。
我正在尝试编写一个类来创建可能包含对象或本机类型的文本表示的字符串。基本上,我有
private:
stringstream ss;
public:
template< typename T >
Message& operator<<( const T& value ) {
ss << value;
return *this;
}
重载的 T 类似于int 或者如果类T 定义了方法operator std::string(),我认为我的编译器对此很好。但是,如果T 是某种类型,例如vector<int>,那么它就不再有效,因为vector<int> 没有定义operator std::string()。
我是否可以重载这个运算符,如果T 定义了operator std::string(),那么我打印文本表示,如果没有,我只打印它的地址?
谢谢。
【问题讨论】:
标签: c++ templates operator-overloading