编写“可流动”的类。 [英] Writing classes which are "streamable"

查看:67
本文介绍了编写“可流动”的类。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!


我为我的项目写了一个日志类,我想支持

流语法,我想写的在我的代码中这样:


日志日志(MYPREFIX);


log<< 这是一条消息,正在发送到syslog,value ="

<< 5;


我创建了一个朋友功能:


Log&运营商LT;< (Log& log,std :: string& str);


现在我可以写:


log<< 只支持字符串;


但是这不起作用(当然):


log<< 这将创建两条日志消息 << "为什么这不是

连接?" ;;


我想知道是否有人知道一些可以帮助我的文件

使我的日志类与流兼容。

问候,


Mikael

Hi!

I have written a logging class for my project and I want to support the
stream syntax, that is I would like to write like this in my code:

Log log("MYPREFIX");

log << "This is a message, which is being sent to syslog, value ="
<< 5;

I have created a friend function:

Log& operator<< (Log& log, std::string& str);

So now I can write:

log << "Only strings are supported";

However this doesn''t work (of course):

log << "This will create two log messages" << " why isn''t this
concatenated?";

I wonder if someone knows about some documentation which could aid me
in making my log class stream-compatible.
Regards,

Mikael

推荐答案

Mikael写道:
Mikael wrote:
嗨!

我为我的项目写了一个日志类,我想支持
stream语法,我想在我的代码中这样写:

日志日志(MYPREFIX);

log<< 这是一条消息,正在发送到syslog,value ="
<< 5;

我创建了一个朋友功能:

Log&运营商LT;< (Log& log,std :: string& str);


你确定你的功能需要修改str吗?如果没有,为什么它不是

const引用?

现在我可以写:

log<< 只支持字符串;

然而,这当然不起作用:

log<< 这将创建两条日志消息 << "为什么这不会连接?" ;;


你的意思是什么都不行,为什么当然?

我想知道是否有人知道某些文件这可以帮助我
使我的日志类流兼容。
Hi!

I have written a logging class for my project and I want to support the
stream syntax, that is I would like to write like this in my code:

Log log("MYPREFIX");

log << "This is a message, which is being sent to syslog, value ="
<< 5;

I have created a friend function:

Log& operator<< (Log& log, std::string& str);
Are you sure that your function needs to modify str? If not, why isn''t it a
const reference?
So now I can write:

log << "Only strings are supported";

However this doesn''t work (of course):

log << "This will create two log messages" << " why isn''t this
concatenated?";
What do you mean by "doesn''t work", and why "of course"?
I wonder if someone knows about some documentation which could aid me
in making my log class stream-compatible.




你可以从std :: ostream派生它,它将自动为你提供所有

为标准输出流定义的流操作符。



You could derive it from std::ostream, which will automatically give you all
the stream operators defined for the standard output streams.




Mikael写道:

Mikael wrote:
嗨!

我已经为我的项目编写了一个日志类,我想支持
流语法,我想在我的代码中这样编写:

日志日志(MYPREFIX);

log<< 这是一条消息,正在发送到syslog,value ="
<< 5;

我创建了一个朋友功能:

Log&运营商LT;< (Log& log,std :: string& str);

所以现在我可以写:

log<< 只支持字符串;

但是这不起作用(当然):


发布更多代码,


Log&运营商LT;< (Log& log,std :: string& str);


返回???
log<< 这将创建两条日志消息 << "为什么这不是连接的?" ;;

我想知道是否有人知道一些文件可以帮助我
使我的日志类流兼容。 />
问候,

Mikael
Hi!

I have written a logging class for my project and I want to support the
stream syntax, that is I would like to write like this in my code:

Log log("MYPREFIX");

log << "This is a message, which is being sent to syslog, value ="
<< 5;

I have created a friend function:

Log& operator<< (Log& log, std::string& str);

So now I can write:

log << "Only strings are supported";

However this doesn''t work (of course):
Post more code, what does

Log& operator<< (Log& log, std::string& str);

return ???
log << "This will create two log messages" << " why isn''t this
concatenated?";

I wonder if someone knows about some documentation which could aid me
in making my log class stream-compatible.
Regards,

Mikael








运营商< ;<定义如下(我已经添加了const

指出):


Log&运营商LT;< (Log& log,std :: string const& str)

{

log.logMessage(str.c_str());

return记录;

}


对不起,如果我的问题太不明确,我会在下次尝试更详细




当我写下

以下代码时,上面的函数生成两条日志消息:


log<< ; 消息1 << " message 2"


因为运算符<<()直接调用log.logMessage()。


如果我派生自std :: ostream,我如何将格式化的

字符串放入我的日志中?如果你能说明它应该如何运作

那么?


如果这些都是愚蠢的问题,我很抱歉,我对溪流很陌生(我是/>
只使用了它们。

亲切的问候,


Mikael


The operator<< is defined as follows (I have added the const as you
pointed out):

Log& operator<< (Log& log, std::string const& str)
{
log.logMessage(str.c_str());
return log;
}

Sorry if my question was too unspecific, I will try to be more verbose
the next time.

The above function generates two log messages when I write the
following code:

log << "message 1" << "message 2";

because operator<<() calls log.logMessage() directly.

If I derive from std::ostream, how do I actually get the formatted
string into my log? If you could please clarify how it should work
then?

I am sorry if these are stupid questions, I am pretty new to streams (I
have only used them sofar).
With kind regards,

Mikael


这篇关于编写“可流动”的类。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆