自己的cout变种 [英] Own Variant of cout

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

问题描述

-----开始PGP签名消息-----

哈希:SHA1


大家好,

我想创建一个自己的cout对象的变体,这样我就可以使用

来调试那些......


喜欢这个课程,但是作为一个小溪...


class Log

{

private:

static int system_debuglevel;

int debuglevel;

public:

Log(int dl):debuglevel(dl){}

void logthis(const char * msg)

{

if(debuglevel< = system_debuglevel)printf("%s",msg); < br $>
}


}


这样我就可以创建像这样的全局对象:

调试(5);

信息(4);

警告(3);

错误(2);


然后可以做Debug<< " DEBUGMESSAGE" << ENDL;与各种具有ostream&的
对象运算符<<()..

所以我可能需要一些帮助,我试图从ostream派生

记录器,我尝试过模板函数,但是什么都没有

工作真的很好......然后该类应该做出输出决定

Debug<< "字符串" << 5<< 4.5f<< ENDL;正确,但它总是

仅输出字符串,或者总是5和4.5,不管是什么?

system_debuglevel设置...我可能需要一些帮助...


迎接


丹尼斯


-----开始PGP SIGNATURE -----

版本:PGP 7.0.1

iQA / AwUBP4VyFhL69PVFlLwmEQLhswCgtBt1938ibhAXIKdiRjEDan 1AYwsAn0Nq

UUT + Cq0VLMAq9pRd9twbtN0C

= fsht

----- END PGP SIGNATURE -----

解决方案

嗨Dennis,
|我想创建一个自己的cout对象变体,这样我就可以使用

|不同的调试......

|

|喜欢这个课程,但作为一个流...

|

|班级日志

| {

|私人:

| static int system_debuglevel;

| int debuglevel;

|公众:

| Log(int dl):debuglevel(dl){}

| void logthis(const char * msg)

| {

| if(debuglevel< = system_debuglevel)printf("%s",msg);

| }

|

| }

....

|然后可以执行Debug<< " DEBUGMESSAGE" << ENDL;各种各样的

|具有ostream&运算符<<()..

|所以我可能需要一些帮助,我试图推导出

|来自ostream的记录器,我已经尝试过模板功能,但没有什么

|工作真的很好......然后班级应该做出

|的输出决定调试<< "字符串" << 5<< 4.5f<< ENDL;正确,但它总是

|输出只有字符串,或总是5和4.5,无论如何

| system_debuglevel已设置...我可能需要一些帮助...


尝试将以下公共成员函数添加到Log类

,因为它在上面定义:


模板< typename T>

Log& operator<(const& param)

{

if(debuglevel< = system_debuglevel)

cout<< param;

返回*这个;

}


我希望这可行。

如果不是,请告诉我这是什么行为...

hth - Ivan

-
http://ivan.vecerina.com

Brainbench MVP for C ++<> http://www.brainbench.com




我认为你应该能够从ostream中派生一个类,但是你需要

来正确地与ostream缓冲区接口我想想。


一个更简单的方法是忘记ostream并使用你现在拥有的课程。

然后将所有流媒体功能拉入你的班级模板memer

功能(当然在标题中)


class CEnd

{

};

extern CEnd End;


模板< class T>堵塞和放大器; opeartor<(T值)

{

if(DebugLevel< SystemDebugLevel)cerr<<价值;

}


模板<>堵塞和放大器;运算符<<(CEnd& End)

{

cerr<<结束;

}


....如果你想要更多专业化


我是从我可能在某个地方犯了错误。

现在你可以使用你的代码但是应该使用End而不是endl


这就是这种方式我在我的课程中这样做。


问候,Ron AF Greve

" Dennis Lubert" <卢**** @ sipoc.de>在消息中写道

news:bm ************* @ news.t-online.com ...

---- -BEGIN PGP签名消息-----
哈希:SHA1

大家好,

我想创建一个自己的cout对象变体,这样我可以使用不同的那些进行调试......

喜欢这个类,但是作为一个流...

类日志
{<私有:
static int system_debuglevel;
int debuglevel;
public:
Log(int dl):debuglevel(dl){}
void logthis(const char * msg)
{
if(debuglevel< = system_debuglevel)printf("%s",msg);
}

}

这样我就可以创建像这样的全局对象:

Debug(5);
Info(4);
警告(3);
错误(2);

然后可以做Debug<< " DEBUGMESSAGE" << ENDL;与各种具有ostream&和/或运算符<<()..
所以我可能需要一些帮助,我试图从ostream派生
记录器,我尝试过模板功能,但没有什么工作真的很好...然后该类应该为
Debug<< "字符串" << 5<< 4.5f<< ENDL;正确,但它始终只输出字符串,或总是5和4.5,无论如何设置system_debuglevel ......我可能需要一些帮助...

greets
丹尼斯

-----开始PGP签名-----
版本:PGP 7.0.1

iQA / AwUBP4VyFhL69PVFlLwmEQLhswCgtBt1938ibhAXIKdiRjEDan 1AYwsAn0Nq < br> UUT + Cq0VLMAq9pRd9twbtN0C
= fsht
-----结束PGP SIGNATURE -----



< blockquote> Moonlit写道:



我认为你应该能够从ostream派生一个类但是你有正确的界面
我认为使用ostream缓冲区。

更简单的方法是忘记ostream并使用你现在拥有的类。
然后使用模板memer将所有流功能拉入你的班级
功能(当然在标题中)

类CEnd
{
};
extern CEnd End;

模板< ; T类>堵塞和放大器; opeartor<(T值)
{
if(DebugLevel< SystemDebugLevel)cerr<<价值;
}

模板<>堵塞和放大器;运算符<<(CEnd& End)
{
cerr<<如果你想要更多的专业化

我是从头脑中做到的,所以我可能在某个地方犯了错误。
现在你可以使用你的代码但是应该使用End而不是endl

这就是我在我的程序中这样做的方式。

问候,Ron AF Greve




1.不要过头。热门帖子是您将回复放在最前面

的原始帖子。回复是散布或附加

到底部。请修理你的新闻阅读器。


2.看到这个链接:
http://www.jelovic.com/articles/stupid_naming.htm


-

Thomas Matthews


C ++新闻组欢迎辞:
http://www.slack.net/~shiva/welcome.txt

C ++常见问题: http://www.parashift.com/c++-faq-lite

C常见问题:< a rel =nofollowhref =http://www.eskimo.com/~scs/c-faq/top.htmltarget =_ blank> http://www.eskimo.com/~scs/c -faq / top.html

alt.comp.lang.learn.c-c ++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html

其他网站:
http://www.josuttis.com - C ++ STL图书馆书籍


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi people,

I want to create my own variant of a cout Object, so that I can use
different of those for debugging...

Like this class, but as a stream...

class Log
{
private:
static int system_debuglevel;
int debuglevel;
public:
Log( int dl ) : debuglevel(dl) { }
void logthis( const char * msg)
{
if ( debuglevel <= system_debuglevel ) printf("%s", msg);
}

}

So that I can create global objects like those :

Debug(5);
Info(4);
Warn(3);
Error(2);

and can then do Debug << "DEBUGMESSAGE" << endl; with every kind of
object that has a ostream& operator<<()..
So I could need some help with this, I have tried to derive the
logger from ostream, I have tried template functions, but nothing
works really good... the class should then do the output decision for
Debug << "String" << 5 << 4.5f << endl; correctly, but it always
outputs only String, or always 5 and 4.5 regardless whaat
system_debuglevel is set... I could need some help...

greets

Dennis

-----BEGIN PGP SIGNATURE-----
Version: PGP 7.0.1

iQA/AwUBP4VyFhL69PVFlLwmEQLhswCgtBt1938ibhAXIKdiRjEDan 1AYwsAn0Nq
UUT+Cq0VLMAq9pRd9twbtN0C
=fsht
-----END PGP SIGNATURE-----

解决方案

Hi Dennis,
| I want to create my own variant of a cout Object, so that I can use
| different of those for debugging...
|
| Like this class, but as a stream...
|
| class Log
| {
| private:
| static int system_debuglevel;
| int debuglevel;
| public:
| Log( int dl ) : debuglevel(dl) { }
| void logthis( const char * msg)
| {
| if ( debuglevel <= system_debuglevel ) printf("%s", msg);
| }
|
| }
....
| and can then do Debug << "DEBUGMESSAGE" << endl; with every kind of
| object that has a ostream& operator<<()..
| So I could need some help with this, I have tried to derive the
| logger from ostream, I have tried template functions, but nothing
| works really good... the class should then do the output decision for
| Debug << "String" << 5 << 4.5f << endl; correctly, but it always
| outputs only String, or always 5 and 4.5 regardless whaat
| system_debuglevel is set... I could need some help...

Try adding the following public member function to the Log class
as it is defined above:

template<typename T>
Log& operator<<(const T& param)
{
if ( debuglevel <= system_debuglevel )
cout << param;
return *this;
}

I would expect this to work.
If it doesn''t, please let me know what the behavior is...
hth - Ivan
--
http://ivan.vecerina.com
Brainbench MVP for C++ <> http://www.brainbench.com


Hi,

I think you should be able to derive a class from ostream but then you have
to correctly interface with the ostream buffers I think.

An easier way is to forget about the ostream and use the class you now have.
Then pull all the streaming functions into your class with a template memer
function (goes in the header of course)

class CEnd
{
};
extern CEnd End;

template<class T> CLog& opeartor<<( T Value )
{
if( DebugLevel < SystemDebugLevel ) cerr << Value;
}

template<> CLog& operator<<( CEnd& End )
{
cerr << endl;
}

.... Some more specializations if you want

I do this from the top of my head so I might have make a mistake somewhere.
Now you can use your code but should use End instead of endl

This is the way I do it in my programs.

Regards, Ron AF Greve
"Dennis Lubert" <lu****@sipoc.de> wrote in message
news:bm*************@news.t-online.com...

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi people,

I want to create my own variant of a cout Object, so that I can use
different of those for debugging...

Like this class, but as a stream...

class Log
{
private:
static int system_debuglevel;
int debuglevel;
public:
Log( int dl ) : debuglevel(dl) { }
void logthis( const char * msg)
{
if ( debuglevel <= system_debuglevel ) printf("%s", msg);
}

}

So that I can create global objects like those :

Debug(5);
Info(4);
Warn(3);
Error(2);

and can then do Debug << "DEBUGMESSAGE" << endl; with every kind of
object that has a ostream& operator<<()..
So I could need some help with this, I have tried to derive the
logger from ostream, I have tried template functions, but nothing
works really good... the class should then do the output decision for
Debug << "String" << 5 << 4.5f << endl; correctly, but it always
outputs only String, or always 5 and 4.5 regardless whaat
system_debuglevel is set... I could need some help...

greets

Dennis

-----BEGIN PGP SIGNATURE-----
Version: PGP 7.0.1

iQA/AwUBP4VyFhL69PVFlLwmEQLhswCgtBt1938ibhAXIKdiRjEDan 1AYwsAn0Nq
UUT+Cq0VLMAq9pRd9twbtN0C
=fsht
-----END PGP SIGNATURE-----



Moonlit wrote:

Hi,

I think you should be able to derive a class from ostream but then you have
to correctly interface with the ostream buffers I think.

An easier way is to forget about the ostream and use the class you now have.
Then pull all the streaming functions into your class with a template memer
function (goes in the header of course)

class CEnd
{
};
extern CEnd End;

template<class T> CLog& opeartor<<( T Value )
{
if( DebugLevel < SystemDebugLevel ) cerr << Value;
}

template<> CLog& operator<<( CEnd& End )
{
cerr << endl;
}

... Some more specializations if you want

I do this from the top of my head so I might have make a mistake somewhere.
Now you can use your code but should use End instead of endl

This is the way I do it in my programs.

Regards, Ron AF Greve



1. Don''t top-post. Top-posting is where you put your response "on top"
of the original post. Replies are either interspersed or appended
to the bottom. Please fix your newsreader.

2. See this link:
http://www.jelovic.com/articles/stupid_naming.htm

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book


这篇关于自己的cout变种的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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