cout << "11122333" << endl;
期待:
11122333 \ n
结果:
11122333 \ n
所有权利。
代码:
cout.operator<<("11122333");
cout.operator<<(endl);
期待:
11122333 \ n
结果:
00B273F8 \ n
(或其他地址,它转换为void*
:()
麻烦: 想要写出来的类从ostream
class SomeStream : public ostream
{
public:
explicit SomeStream(streambuf* sb) : ostream(sb) { }
template <typename T> SomeStream &operator <<(const T &val)
{
std::ostream::operator<<(val); //Trouble in there!
std::cout << "<<" << " " << typeid(T).name() << " " << val << std::endl;
/*some other behavior*/
return *this;
}
SomeStream &operator <<(ostream& (*val) (ostream&))
{
std::ostream::operator<<(val);
/*some other behavior*/
return *this;
}
SomeStream &operator <<(ios_base& (*val) (ios_base&))
{
std::ostream::operator<<(val);
/*some other behavior*/
return *this;
}
};
当我打电话给家长操作员std::ostream::operator<<(val);
val cast到void*
而不是正常工作。 如何正确做?以及为什么直接致电operator<<
的ostream
与间接呼叫不一样。
输出'运营商<<()'是一个全球性的模板函数不'的std :: ostream'的成员。 –
'(cout.operator <<)(“11122333”);'? – 2016-01-13 12:36:07
@Kilanny - 它实际上是'std :: operator <<(cout,“11122333”);'。 –