子类化得到<<和>> [英] Subclassing to get << and >>

查看:69
本文介绍了子类化得到<<和>>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉这个蹩脚的头衔,但很难浓缩我要做的事情

说...这是我的情况(我的第一个真正的C ++情况,yay !)。

目前我们有一个MyTCPThread(不要让名字吓到你,这个

不是OT)有一个套接字成员对象有方法Send()和

GetLine()用于发送和接收数据。我想继承

MyTCPThread所以我可以使用<<和>>作为Send()的包装器和

GetLine()(发送()是我目前最关心的问题,FWIW)。我可以

可能会拿出一些陈腐的计划来自己做这件事,但是

我真的想要一些关于如何做好的建议。 />

我最初的新手想法提示类似


class

MyStreamBasedTCPThread:public MyTCPThread

{

受保护:

std :: ostringstream outbuf;

};


但是大我看到的问题是如何使<<适用于outbuf

(将被发送)并仍然保留所有酷的重载<<

运营商ostringstreams已经拥有。我真的感到很失落

寻找一个很好的方法来做到这一点。我将非常感谢任何

指导!


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。

Sorry for the lame title, but it''s hard to condense what I''m about to
say... Here''s my situation (my first real C++ situation, yay!).
Currently we have a MyTCPThread (don''t let the name scare you, this
isn''t OT) that has a socket member object that has methods Send() and
GetLine() for sending and receiving data. I want to subclass
MyTCPThread so I can use << and >> as wrappers for Send() and
GetLine() (Send() is my primary concern at the moment, FWIW). I could
probably come up with some hackneyed scheme to do this on my own, but
I''d really like some advice on how to do it well.

My initial newbie thoughts suggest something like

class
MyStreamBasedTCPThread : public MyTCPThread
{
protected:
std::ostringstream outbuf;
};

but the big problem I see with this is how to make << apply to outbuf
(which will get sent) and still keep all the cool overloaded <<
operators that ostringstreams already have. I''m really feeling lost
looking for a good way to do this. I''d be extremely grateful for any
guidance!

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.

推荐答案

2004-02-20 14:04 +0000,Christopher Benson-Manica写道:
At 2004-02-20 14:04 +0000, Christopher Benson-Manica wrote:
对于蹩脚的标题感到抱歉,但很难浓缩我要说的话
说...这是我的情况(我的第一个真正的C ++情况,是的!)。
目前我们有一个MyTCPThread(不要让名字吓到你,这不是OT),它有一个socket成员对象,它有方法Send()和
GetLine()用于发送和接收数据。我想继承
MyTCPThread所以我可以使用<<和>>作为Send()和
GetLine()的包装器(发送()是我目前最关心的问题,FWIW)。我可能会想出一些陈腐的计划来自己做这件事,但是我真的想要一些关于如何做好的建议。

我的初期新手的想法提示类似


MyStreamBasedTCPThread:public MyTCPThread
{
受保护:
std :: ostringstream outbuf;
}; <但是,我看到的最大问题是如何制作<<适用于outbuf
(将被发送)并仍然保留ostringstreams已经拥有的所有酷的重载<<
运算符。我真的感到很失落
寻找一个好方法来做到这一点。我非常感谢任何
指导!
Sorry for the lame title, but it''s hard to condense what I''m about to
say... Here''s my situation (my first real C++ situation, yay!).
Currently we have a MyTCPThread (don''t let the name scare you, this
isn''t OT) that has a socket member object that has methods Send() and
GetLine() for sending and receiving data. I want to subclass
MyTCPThread so I can use << and >> as wrappers for Send() and
GetLine() (Send() is my primary concern at the moment, FWIW). I could
probably come up with some hackneyed scheme to do this on my own, but
I''d really like some advice on how to do it well.

My initial newbie thoughts suggest something like

class
MyStreamBasedTCPThread : public MyTCPThread
{
protected:
std::ostringstream outbuf;
};

but the big problem I see with this is how to make << apply to outbuf
(which will get sent) and still keep all the cool overloaded <<
operators that ostringstreams already have. I''m really feeling lost
looking for a good way to do this. I''d be extremely grateful for any
guidance!




嗯,我不确定我明白你要做什么,但是会不会像以下那样做你想要的东西?


#include< string>

#include< iostream>

#include< sstream>


class foo

{

public:

void send(const std :: string& str)

{std :: cout<< 发送: << str<< \\\
; }

};


模板< class T>

foo& operator<<(foo& s,const T& t)

{

std :: ostringstream ss;

ss<< t;

s.send(ss.str());

返回s;

}


int main()

{

foo f;


f<< 你好!\ n;

f<< 2 + 2 = << 2 + 2<< ''\\ n';;

}



Well I''m not sure I understand what you''re trying to do, but would
something like the following do what you want?

#include <string>
#include <iostream>
#include <sstream>

class foo
{
public:
void send(const std::string& str)
{ std::cout << "Sending: " << str << ''\n''; }
};

template <class T>
foo& operator<<(foo& s, const T& t)
{
std::ostringstream ss;
ss << t;
s.send(ss.str());
return s;
}

int main()
{
foo f;

f << "Hello!\n";
f << "2 + 2 = " << 2 + 2 << ''\n'';
}


fifo写道:
fifo wrote:
2004- 02-20 14:04 +0000,Christopher Benson-Manica写道:
At 2004-02-20 14:04 +0000, Christopher Benson-Manica wrote:
对于蹩脚的头衔感到抱歉,但很难浓缩我要说的话。 ..这是我的情况(我的第一个真正的C ++情况,是的!)。
目前我们有一个MyTCPThread(不要让这个名字吓到你,这个
不是OT)它有一个socket成员对象,它有方法Send()和GetLine()来发送和接收数据。我想继承
MyTCPThread所以我可以使用<<和>>作为Send()和
GetLine()的包装器(发送()是我目前最关心的问题,FWIW)。我可能会想出一些陈腐的计划来自己做这件事,但我真的想要一些关于如何做好的建议。

我的初期新手的想法提示类似


MyStreamBasedTCPThread:public MyTCPThread
{
受保护:
std :: ostringstream outbuf;
}; <但是,我看到的最大问题是如何制作<<适用于outbuf
(将被发送)并仍然保留ostringstreams已经拥有的所有酷的重载<<
运算符。我真的感到很失落
寻找一个好方法来做到这一点。我非常感谢任何
指导!

我不确定我明白你想要做什么,但是会像
那样以下做你想做的事情?

#include< string>
#include< iostream>
#include< sstream>

类foo
{
公开:
void send(const std :: string& str)
{std :: cout<< 发送: << str<< \\\
;模板< class T>
foo& operator<(foo& s,const T& t)
{st / :: ostringstream ss;
ss<< t;
s.send(ss.str());
返回s;
}



foo f;

f<< 你好!\ n;
f<< 2 + 2 = << 2 + 2<< ''\ n'';
Sorry for the lame title, but it''s hard to condense what I''m about to
say... Here''s my situation (my first real C++ situation, yay!).
Currently we have a MyTCPThread (don''t let the name scare you, this
isn''t OT) that has a socket member object that has methods Send() and
GetLine() for sending and receiving data. I want to subclass
MyTCPThread so I can use << and >> as wrappers for Send() and
GetLine() (Send() is my primary concern at the moment, FWIW). I
could probably come up with some hackneyed scheme to do this on my
own, but I''d really like some advice on how to do it well.

My initial newbie thoughts suggest something like

class
MyStreamBasedTCPThread : public MyTCPThread
{
protected:
std::ostringstream outbuf;
};

but the big problem I see with this is how to make << apply to outbuf
(which will get sent) and still keep all the cool overloaded <<
operators that ostringstreams already have. I''m really feeling lost
looking for a good way to do this. I''d be extremely grateful for any
guidance!

Well I''m not sure I understand what you''re trying to do, but would
something like the following do what you want?

#include <string>
#include <iostream>
#include <sstream>

class foo
{
public:
void send(const std::string& str)
{ std::cout << "Sending: " << str << ''\n''; }
};

template <class T>
foo& operator<<(foo& s, const T& t)
{
std::ostringstream ss;
ss << t;
s.send(ss.str());
return s;
}

int main()
{
foo f;

f << "Hello!\n";
f << "2 + 2 = " << 2 + 2 << ''\n'';




将其更改为:


f<< 2 + 2 = << 2 + 2<< std :: endl;


尝试再次编译它。

}



Change that into:

f << "2 + 2 = " << 2 + 2 << std::endl;

adn try to compile it again.
}




- -

首先他们不理你。然后他们就你笑了。

然后他们就打你了。然后你就赢了。

(圣雄甘地)



--
First they ignore you. Then they laugh about you.
Then they fight you. And then you win.
(Mahatma Gandhi)




" Christopher Benson-Manica" ; <在*** @ nospam.cyberspace.org>在消息中写道

新闻:c1 ********** @ chessie.cirr.com ...

"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:c1**********@chessie.cirr.com...
对不起蹩脚的头衔,但它'我很难压缩我要说的......这就是我的情况(我的第一个真正的C ++情况,是的!)。
目前我们有一个MyTCPThread(不是''''''让名字吓到你,这个没有OT),它有一个套接字成员对象,它有方法Send()和GetLine()来发送和接收数据。我想继承
MyTCPThread所以我可以使用<<和>>作为Send()和
GetLine()的包装器(发送()是我目前最关心的问题,FWIW)。我可能会想出一些陈腐的计划来自己做这件事,但是我真的想要一些关于如何做得好的建议。
Sorry for the lame title, but it''s hard to condense what I''m about to
say... Here''s my situation (my first real C++ situation, yay!).
Currently we have a MyTCPThread (don''t let the name scare you, this
isn''t OT) that has a socket member object that has methods Send() and
GetLine() for sending and receiving data. I want to subclass
MyTCPThread so I can use << and >> as wrappers for Send() and
GetLine() (Send() is my primary concern at the moment, FWIW). I could
probably come up with some hackneyed scheme to do this on my own, but
I''d really like some advice on how to do it well.




[SNIP]


恕我直言,你这是因为如果你提供<<和>>

然后类用户将希望完全按照他们习惯的方式使用流插入器和提取器

。正如Rolf已经表达了他的评论

这可能会成为一个非常繁琐的业务,从流操作符开始

(如std :: endl)。可能最简单的方法是使用Send& amp; Getline()

方法。


干杯

克里斯



[SNIP]

IMHO you''re walking on thin ice with this because if you supply << and >>
then the class user will want to use the stream inserters and extractors
exactly the way they are used to. As Rolf already showed with his remark
this might become a very tedious business starting with stream manipulators
(like std::endl). Probably the easiest way is to stay with Send & Getline()
methods.

Cheers
Chris


这篇关于子类化得到&lt;&lt;和&gt;&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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