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」);'。 –