构建我自己的类,其行为类似于输出字符串流 [英] build my own class that behaves like an output string stream
问题描述
Hello Everybody,
我希望我自己的班级表现得像下面的oss对象;
------- -----------------代码开始-----
int main(){
int i = 67;
ostringstream oss;
oss<< " A" << i<< endl;
}
------------------------代码结束----- -
这意味着我希望能够在我的班级中使用''<<'''运算符,当然在课程中我希望能够访问传递的流。小心,我不是在找一个类似于输出的课程
cout<< one_instance_of_my_class。
要做到这一点,我想以下列方式重载''<<<'''运算符:
--- ---------------------代码开始-----
类表:public ostringstream
{
public:
friend Table&
operator<<(Table& tab,ostringstream& os){
cout<< 这是装载的: << os.str()<< endl;
tab.content + = os.str();
返回标签;
}
私人:
字符串内容;
}
------------------------代码结束------
用途如下:
------------------------代码开始-----
int main(){
int i = 67;
表mytab;
mytab<< " A" << i<< endl;
}
------------------------代码结束----- -
如果我保留基类ostringstream,则不会执行重载的运算符,因此我无法访问传递的流。
如果省略基类,例如通过的A表示在''<<'''运算符的使用中不被视为字符串流,而是字符串。
我有意识到这可能是一个微不足道的c ++问题,但首先,我正在寻找一段时间,并没有找到任何方法来做到这一点,其次,我是新的c ++: - /。
提前谢谢非常感谢您的帮助。
VBR
gato
Hello Everybody,
I want my own class to behave like the following oss object;
------------------------ code begin -----
int main () {
int i = 67;
ostringstream oss;
oss << "A" << i << endl;
}
------------------------ code end ------
which means that i want to be able to use the ''<<'' operator with my class, and of course in the class i would like to have access to the passed stream. Caution, i''m not looking for a class which is outputstreamable like
cout << one_instance_of_my_class.
To do this i thought overloading the ''<<'' operator in the following way:
------------------------ code begin -----
class Table : public ostringstream
{
public:
friend Table&
operator<<(Table& tab, ostringstream& os) {
cout << "this is beeing loaded: " << os.str() << endl;
tab.content += os.str();
return tab ;
}
private:
string content;
}
------------------------ code end ------
to be uses like :
------------------------ code begin -----
int main () {
int i = 67;
Table mytab;
mytab << "A" << i << endl;
}
------------------------ code end ------
If i keep the base class ''ostringstream'' the overloaded operator is not executed, thus i have no access to the passed stream.
If I omit the base class, e.g. the passed "A" in the use of the ''<<'' operator is not seen as a stringstream, but as string.
I''m conscious that this might be a somehow trivial c++ question, but firstly, i was looking for a while and did not found any way to do this and secondly, i''m pretty new to c++ :-/.
Thanks in advance very much for your kind help.
VBR
gato
推荐答案
Hello Everybody,
在以下代码中,
int main(){
ostringstream oss;
oss<< A;
}
Hello Everybody,
In the following code,
int main () {
ostringstream oss;
oss << "A";
}
所以???有什么问题?!!
So??? What''s the problem?!!
Hello Everybody,
以下代码,
int main(){
ostringstream oss;
oss<< ; A;
}
Hello Everybody,
In the following code,
int main () {
ostringstream oss;
oss << "A";
}
你想要''为了构建你自己的接口为ostrigstream的类,对吗?
好吧,然后从运营商<<开始,他是你成功的关键。
>
Savage
PS:请阅读我们的发布指南..
You wan''t to build your own class that has interface as ostrigstream,right?
Well,then start from operator <<,he''s the key to your success.
Savage
PS:Please read our posting guidelines..
所以???有什么问题?!!
So??? What''s the problem?!!
对不起,我做了键盘错误并且发送的信息太早了。
对不起!
gato
Sorry, i did a keyboard error and sent the message too early.
Sorry for that!
gato
这篇关于构建我自己的类,其行为类似于输出字符串流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!