ostream 运算符<<调用父 ostream
ostream operator<< call parent ostream
代码:
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转换为void*
,而不是正常工作。怎么做才对?以及CCD_ 6的直接呼叫CCD_。
const char*
的输出operator <<
不是类型ostream
的成员。只有那些重载是成员函数,其中一个重载用于void*
。还有非成员重载。
有一种变通方法:
template <typename T> SomeStream &operator <<(const T &val)
{
static_cast<std::ostream&>(*this) << val; //Trouble in there!
std::cout << "<<" << " " << typeid(T).name() << " " << val << std::endl;
/*some other behavior*/
return *this;
}
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- ostream过载时的缓冲区冲洗
- 需要从 istream 和 ostream 派生 iostream
- 如何防止clang格式在流运算符调用之间添加换行符<<
- <<操作员在下面的行中工作
- "ostream &os"有什么用?
- 为什么常量词在重载运算符中不与 ostream 对象一起使用<<?
- 我的运算符重载是否有效<<(流插入)左操作数不是 ostream
- C++表达SFINAE和ostream操纵器
- 在抛出 'std::runtime_error' 的实例后终止调用 what(): Filebuf 和 ostream 的 I/O 错误
- oStream 不打印添加两个 valarray 的结果(使用运算符重载)
- C++运算符<<调用::ostream而不是std::osttream
- BOOST ::变体无法解决运算符&lt;&lt;对于STD :: Ostream
- C++ostream:没有运算符匹配<<&应在'&'代币
- 重载运算符<<用于ostream语法
- 重载ostream&时转换错误无效;operator< & lt;
- 运算符<<(ostream&os,..)用于模板类
- std::ostream&operator< & lt; (std:: ostream&压力,压力& &;val)
- 上Ostream & lt; & lt;重载的困惑