C++ OStream 继承和操纵器
C++ OStream Inheritance and Manipulators
我有一个继承自ostream
但操纵器无法编译的类。例如:
QLogger &logger = QLogger::getInstance();
logger << hex << 10 << endl;
抱怨logger << hex
.如果我将记录器投到ostream
它会起作用,但这非常笨拙。
如何扩展我的类,使其表现为带有操纵器的ostream
?
谢谢
肯尼
class LIBEXPORT QLogger: public std::ostream
{
friend class boost::thread_specific_ptr< QLogger >;
friend class QLoggerFunnel;
public:
// Priority for message filtering.
//
// DEBUG Detailed information that may be useful during development or troubleshooting.
// NORMAL Message will be useful during normal operation.
// WARNING Message indicates an unhealthy condition but the system should continue to operate.
// FAULT Message indicates a critical error. The system may not operate normally.
enum Priority
{
DEBUG, NORMAL, WARNING, FAULT
};
// DEPRECATED: Defined for backward compatibility.
static const Priority LOW = NORMAL;
static const Priority HIGH = WARNING;
static const Priority URGENT = FAULT;
// Returns a reference to the global instance.
static QLogger& getInstance();
// DEPRECATED: Use instead: QLogger::instance() << "Example message." << std::endl
static AWI_DEPRECATED QLogger& stream( QLogger::Priority priority = NORMAL );
QLogger &operator<<( Priority p );
// Messages with a priority below the threshold are discarded.
void setThreshold( Priority priority );
Priority getThreshold( void ) const;
// Set the priority of messages.
void setPriority( Priority priority );
Priority getPriority( void ) const;
protected:
static void handleThreadExit( QLogger *logger );
QLogger();
QLogger( const QLogger& );
virtual ~QLogger();
QLogger& operator=( const QLogger& );
void write( std::ostream &destination );
void synchronize( void );
// Prepends information to each line of its associated output stream.
class StampBuffer: public std::stringbuf
{
public:
StampBuffer();
virtual ~StampBuffer();
// String to be displayed before each line.
void setPreamble( const char *p );
virtual int sync();
void write( std::ostream &destination );
protected:
boost::mutex _mutex;
std::stringstream _stream1;
std::stringstream _stream2;
// Active stream being written to.
std::stringstream *_write;
std::stringstream *_read;
const char *_preamble;
};
class NullBuffer: public std::stringbuf
{
public:
NullBuffer();
virtual ~NullBuffer();
virtual int overflow( int c )
{
return (c);
}
};
StampBuffer _buffer;
NullBuffer _null_buffer;
Priority _threshold;
Priority _priority;
};
如果实现成员函数,则在重载解析中不考虑同一函数的继承重载。如果将自定义operator<<
重载更改为友元全局函数而不是成员,这应该可以正常工作。强制转换强制编译从 ostream
中定义的那些中选择operator<<()
。
不需要函数指针的特殊重载。
相关文章:
- 继承函数的重载解析
- 继承期间显示未知行为的子类
- 头文件-继承c++
- 为什么在保护模式下继承升级不起作用
- 通过继承类使用来自不同命名空间的运算符
- 子目录是否继承属性,例如add_definitions,include_directories和父Cmakelist.t
- 混合组合和继承的C++问题
- 继承:构造函数,初始化C++11中基类的类C数组成员
- 从类继承时,继承的类是否会通过父类重新定义继承的变量
- 公共与私人继承
- 如何创建从同一类继承的不同对象的向量
- 如何从另一个文件继承私有成员变量和公共函数
- C++setiosflags函数操纵器-未确定的缩进
- 在模板基类中为继承类中的可选重写生成虚拟方法
- 带有继承的C++工厂
- 我应该避免多重实现继承吗
- C++继承更改成员
- 从具有默认值的部分指定模板类继承时发生SWIG错误,具有不带默认值的正向声明
- 关于C++中具有多重继承"this"指针的说明
- C++ OStream 继承和操纵器