ostringstream派生的问题 [英] ostringstream derived problem

查看:74
本文介绍了ostringstream派生的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,有人可以帮我这个吗?


我正在尝试缩短项目中的一些日志记录功能,即

而不是


LogString(" ...);


使用更方便


log()<< String here << 12.33<< "和一个数字;


我创建这样的语法:


struct log:public ostringstream {

virtual~log(){cout<< 日志包含: << str()。c_str()<<

endl;}

};


诀窍我是试图做的是log()创建一个类型为

log的对象,然后使用ostringstream'的父级''<<'<''和/
$ b填充它语句结束后$ b被调用析构函数并且消息

被打印到屏幕上。

问题是它没有:

打印类似日志包含:的内容。 4129000

但是如果第一个元素是log()<< "不是字符串,例如,

数字,十进制,十六进制或其他什么,方案成功。


为什么会发生这种情况?我有一种感觉,析构函数可能是在第一个<<<<<<<<<< "但我不明白为什么。


谢谢

Hi all, can someone help me with this?

I''m trying to shorthen some logging function in our project, i.e.,
instead of

LogString("...);

use a more convenient

log() << "String here " << 12.33 << " and a number";

I create this syntax like this:

struct log : public ostringstream {
virtual ~log() { cout << "The log contains: " << str().c_str() <<
endl;}
};

The trick I''m trying to do is that log() creates an object of type
log, then it fills it using the ostringstream''s parent ''<<'' and right
after the statement finishes the destructor is called and the message
is printed onto the screen.
The problem is that it doesn''t:
It prints something like "The log contains: " 4129000
But if the first element after "log() << " is not a string, e.g., a
number, dec, hex or whatever, the scheme succeeds.

Why does this happen? I''ve got a feeling that the destructor might be
called before the first "<< " but I don''t understand why.

Thanks

推荐答案

iu2写道:
iu2 wrote:

大家好,有人可以帮我这个吗?


我正在努力缩短我们的日志记录功能项目,即

而不是


LogString(" ...);


使用更多方便


log()<< String here << 12.33<< "和一个数字;


我创建这样的语法:


struct log:public ostringstream {

virtual~log(){cout<< 日志包含: << str()。c_str()<<

endl;}

};


诀窍我是试图做的是log()创建一个类型为

log的对象,然后使用ostringstream'的父级''<<'<''和/
$ b填充它语句结束后$ b被调用析构函数并且消息

被打印到屏幕上。

问题是它没有:

打印类似日志包含:的内容。 4129000

但是如果第一个元素是log()<< "不是字符串,例如,

数字,十进制,十六进制或其他什么,方案成功。


为什么会发生这种情况?我有一种感觉,析构函数可能是在第一个<<<<<<<<<< "但我不明白为什么。


谢谢
Hi all, can someone help me with this?

I''m trying to shorthen some logging function in our project, i.e.,
instead of

LogString("...);

use a more convenient

log() << "String here " << 12.33 << " and a number";

I create this syntax like this:

struct log : public ostringstream {
virtual ~log() { cout << "The log contains: " << str().c_str() <<
endl;}
};

The trick I''m trying to do is that log() creates an object of type
log, then it fills it using the ostringstream''s parent ''<<'' and right
after the statement finishes the destructor is called and the message
is printed onto the screen.
The problem is that it doesn''t:
It prints something like "The log contains: " 4129000
But if the first element after "log() << " is not a string, e.g., a
number, dec, hex or whatever, the scheme succeeds.

Why does this happen? I''ve got a feeling that the destructor might be
called before the first "<< " but I don''t understand why.

Thanks



非常微妙。我不能告诉你答案,虽然我可以给你一些

指针。


首先它与析构函数无关。临时的析构函数

在包含表达式的末尾被调用。


您看到的数字是字符串String here的地址。 。

请记住,有几个运算符的部分<<其中一些是

成员函数,有些是全局函数。你想要的那个是
就是这个,打印一个字符串


ostream& ostream :: operator<<(const char *)


被调用的是这一个,它打印一个指针地址


ostream& operator<(ostream&,const void *)


换句话说,你的表达式被解释为这样的


运算符<< (log(),String here)<< 12.33<< "当你想要像这样解释它时,可以用一个数字;




log()。operator<<("这里的字符串)<< 12.33<< "和一个数字;


这两个表达式都是合法的,如果你写任何一个,你得到

预期输出(第一种情况下的指针,字符串)在第二个

案例中)。另外如果你写这个

log myLog;

myLog<< String here << 12.33<< "和一个数字;


你得到了所需的字符串输出。


所以问题是为什么有一个临时的派生类

运营商的左侧<<让编译器忽略基类的

的成员函数?


我不知道答案是什么。我很久以前就放弃了这个问题。

。一个聪明人说(我认为这是Bjarne Stroustrup)''保持

远离语言的模糊角落''。这将是我的建议。


我认为使用宏进行日志记录是完全可以接受的。


#define LOG(X) std :: cout<< 日志包含: << X << std :: endl


LOG(" String here"<<< 12.33<<<""&number;")


这也是我的建议。


john

Very subtle. I can''t tell you the answer, although I can give you a few
pointers.

Firstly it''s nothing to do with destructors. Destructors for temporaries
are called at the end of the containing expression.

The number you are seeing is the address of the string "String here ".
Remember that there are several sersions of operator<< some of them are
member functions and some are global functions. The one you want to be
called is this one, which prints a string

ostream& ostream::operator<<(const char*)

the one that is being called is this one, which prints a pointer address

ostream& operator<<(ostream&, const void*)

In other words your expression is being interpretted like this

operator<<(log(), "String here ") << 12.33 << " and a number";

when you would prefer to have it intepreted like this

log().operator<<("String here ") << 12.33 << " and a number";

Both these expression are legal, and if you write either one you get the
expected output (pointer in the first case, string in the second
case). Also if you write this
log myLog;
myLog << "String here " << 12.33 << " and a number";

you get the desired string output.

So the question is why does having a temporary of a derived class on the
left hand side of operator<< make the compiler ignore member function of
the base class?

I have no idea what the answer is. I gave up on this kind of question a
long time ago. A wise man said (I think it was Bjarne Stroustrup) ''stay
away from the obscure corners of the language''. That would be my advice.

I think it''s perfectly acceptable to use macros for logging.

#define LOG(X) std::cout << "The log contains: " << X << std::endl

LOG("String here " << 12.33 << " and a number")

That would also be my advice.

john


>
>

我认为使用宏进行日志记录是完全可以接受的。


#define LOG(X)std: :cout<< 日志包含: << X << std :: endl


LOG(" String here"<<< 12.33<<"""")
I think it''s perfectly acceptable to use macros for logging.

#define LOG(X) std::cout << "The log contains: " << X << std::endl

LOG("String here " << 12.33 << " and a number")



小tpyo,缺少分号


LOG(" String here"<<< 12.33<<""&number;");


john

Small tpyo, missing semi-colon

LOG("String here " << 12.33 << " and a number");

john


7月1日上午8:40,John Harrison< john_androni ... @ hotmail.comwrote:
On Jul 1, 8:40 am, John Harrison <john_androni...@hotmail.comwrote:

我认为使用宏进行日志记录是完全可以接受的。
I think it''s perfectly acceptable to use macros for logging.


#define LOG(X)std :: cout<< 日志包含: << X << std :: endl
#define LOG(X) std::cout << "The log contains: " << X << std::endl


LOG(" String here"<< 12.33<<"" and number)
LOG("String here " << 12.33 << " and a number")



小tpyo,缺少分号


LOG(" String here"<<< 12.33<< 和一个数字;)


john


Small tpyo, missing semi-colon

LOG("String here " << 12.33 << " and a number");

john



谢谢,我会用你的宏观建议。 />

thanks, I''ll use your macro suggestion.


这篇关于ostringstream派生的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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