类的自定义流操纵器
我正在尝试编写一个简单的审计类,它通过运算符 << 获取输入并在收到如下所示的自定义操纵器后编写审核:
class CAudit
{
public:
//needs to be templated
CAudit& operator << ( LPCSTR data ) {
audittext << data;
return *this;
}
//attempted manipulator
static CAudit& write(CAudit& audit) {
//write contents of audittext to audit and clear it
return audit;
}
private:
std::stringstream audittext;
};
//to be used like
CAudit audit;
audit << "Data " << data << " received at " << time << CAudit::write;
我认识到代码中的重载运算符不会返回流对象,但想知道是否仍然可以使用类似操纵器的语法。目前编译器看到的是“<<”作为二进制右移运算符。
感谢您的任何意见, 帕特里克
I am trying to write a simple audit class that takes input via operator << and writes the audit after receiving a custom manipulator like this:
class CAudit
{
public:
//needs to be templated
CAudit& operator << ( LPCSTR data ) {
audittext << data;
return *this;
}
//attempted manipulator
static CAudit& write(CAudit& audit) {
//write contents of audittext to audit and clear it
return audit;
}
private:
std::stringstream audittext;
};
//to be used like
CAudit audit;
audit << "Data " << data << " received at " << time << CAudit::write;
I recognise that the overloaded operator in my code does not return a stream object but was wondering if it was still possible to use a manipulator like syntax. Currently the compiler is seeing the '<<' as the binary right shift operator.
Thanks for any input,
Patrick
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要使其工作,您必须添加运算符 << 的重载对于函数,
而不是从中调用函数:
To make it work you have to add overload of operator << for functions,
than call the function from it:
二进制移位运算符和流运算符是相同的运算符。为您的类重载运算符+以在 std::cout 上写入“Hello world”是完全合法的(尽管这是一个非常糟糕的主意)。就像 C++ 标准作者决定重载运算符<< 一样。对于流来说就像写入流一样。
你没有写清楚你的问题是什么。我的猜测是编译错误。在这种情况下最好的办法是引用错误消息。如果我是对的,问题是,您只定义了运算符<<对于 LPCSTR,然后您希望它在右侧工作函数对象。
你用了“操纵者”这个词,但你误解了一些东西。流操纵器(来自 STL 的流)是一个对其写入的流执行某些操作的函数。它之所以起作用只是因为这种重载:
它接受一个函数并将其应用于流。
同样你需要:
Binary shift operator and stream operator is the same operator. It is completely legal to overload operator+ for your class to write "Hello world" on std::cout (although it is a very bad idea). The same way C++ standard authors decided to overload operator<< for streams as writing to the stream.
You didn't write clearly what is your problem. My guess is compilation error. The best thing in this case is to quote the error message. If I am right, the problem is, that you defined only operator<< for LPCSTR, and then you want it to work function object on the right side.
You use word "manipulator", but you misunderstand something. Manipulator for a stream (stream from STL) is a function that performs some actions on the stream it is written to. And it works only because of this overload:
which takes a function and applies it to a stream.
Similarly you need:
这不就是
你想要的吗?
Wouldn't this
do what you want?
我对跟踪做了一些非常类似的事情,但使用了
stringstream
。这确保了所有第三方操作员<< ()
和操纵器工作。我还使用析构器而不是客户写入操纵器。然后可以通过一些宏来实现:
并且代码只使用宏
您不需要特定的写入操纵器将数据刷新到日志文件。当匿名 DebugStream 对象超出范围时(一旦控制离开该行),内容会自动写入。
尽管在这种情况下我通常会避免使用宏,但使用
if
语句意味着您无需构建跟踪行,除非您确实需要它。通过
stream()
方法返回ostream
使其适用于全局成员函数,因为匿名对象不能作为非常量引用参数传递。I do something very similar for tracing, but use a
stringstream
. This ensures that all 3rd partyoperator << ()
and manipulators work. I also use the desctructor instead of the customer write manipulator.This is then made available with some macros:
And the code just uses the macros
You don't need a specific write manipulator to flush the data to the log file. When the anonymous
DebugStream
object goes out of scope (once control leaves the line) the the contents are automatically written.Although I usually avoid macros in this case the use of the
if
statement means you don't have the overhead of building the trace line unless you actually require it.Returning the
ostream
via thestream()
method enables this to work for global member functions, as anonymous objects cannot be passed as non-const reference parameters.