我正在为两个流写一个包装器。最后,我想模仿与Person
的对话,以使我的代码清晰易用。
声明
class Person
{
public:
void process();
private:
std::stringstream _request;
std::stringstream _response;
}
用法
Person daniel;
std::stringstream answer;
daniel << "Hello" << std::endl; // f.e std::endl means end of the phrase
daniel << "How" << "are you";
daniel << "doing?";
daniel >> answer;
daniel.process();
std::cout << "Daniel says: " << answer;
// or
std::cout << "Daniel says: " << daniel;
我觉得我可以通过重载<<
和>>
运算符来实现它,但我被卡住了。我从来没有为这些目的使用过流。我google了很多,显然仍然无法理解基本的东西。
果壳
std::ostream& operator<<(std::ostream& o, const person& p)
{
output << p._response.rdbuf();
return (output);
}
std::istream& operator>>(std::istream& i, person& p)
{
p._request << i.rdbuf();
// internal process() call probably will be better
return (p._request);
}
确实,有些例子会非常棒,但我会对那些让我摆脱僵局的粗略想法感到非常满意。
答案 0 :(得分:2)
老实说,我不明白为什么你想要这个,但我想这是某种练习或家庭作业。无论如何,实现这个功能有一些细微之处,我怀疑你无法弄明白,所以这是工作解决方案。
#include <iostream>
#include <sstream>
class Person {
public:
void process() {
// Let's see what we've got in `_request`
std::cout << _request.rdbuf() << std::endl;
// Do some processing to produce correponding _response
// In this example we hardcode the response
_response << "I'm fine, thanks!";
}
private:
std::stringstream _request;
std::stringstream _response;
// Make them all friends so that they can access `_request` and `_response`
friend Person& operator<<(Person& p, std::string const& s);
friend Person& operator<<(Person& p, std::ostream& (*f)(std::ostream&));
friend std::ostream& operator<<(std::ostream &o, Person& p);
friend Person& operator>>(Person& p, std::string& s);
};
Person& operator<<(Person& p, const std::string& s) {
p._request << s;
return p;
}
// Notice this peculiar signature, it is required to support `std::endl`
Person& operator<<(Person& p, std::ostream& (*f)(std::ostream&)) {
p._request << f;
return p;
}
// Somewhat conventional stream operator overload (in terms of signature)
std::ostream& operator<<(std::ostream &o, Person& p) {
o << p._response.rdbuf();
return o;
}
Person& operator>>(Person& p, std::string& s) {
// NOTE: This will read not the whole `_reponse` to `s`, but will stop on
// the first whitespace. This is the default behavior of `>>` operator of
// `std::stringstream`.
p._response >> s;
return p;
}
值得一提的是,在您尝试实现的功能方面,您的第一次尝试是完全错误的。这可以归结为这样一个事实:您似乎遵循了流操作符重载的传统教程,而这里的方法应该是不同的,以实现所需的功能。特别要注意建议的流操作符重载的签名。
此外,您必须添加更多重载以支持其他输入类型,例如int
。从本质上讲,您必须添加更多类似于 std::basic_ostream 提供开箱即用的重载。特别要注意最后一个:
basic_ostream& operator<<(basic_ostream& st,
std::basic_ostream& (*func)(std::basic_ostream&));
这家伙是支持std::endl
的。请注意,我已经为您Person
添加了类似的重载,以便std::endl
正常工作(请参阅下文)。其他重载,比如原始类型,都可以作为练习。
你想要的那个。
int main() {
Person daniel;
daniel << "Hello" << std::endl;
daniel << "How " << "are you";
daniel << " doing?" << std::endl;
// We are ready to process the request so do it
daniel.process();
// And Daniel's answer is...
std::cout << "Daniel says: " << daniel << std::endl;
return 0;
}
这个有点笨拙和麻烦,基本上是Person& operator>>(Person& p, std::string& s)
重载实施评论的结果(见上文)。
int main() {
Person daniel;
daniel << "Hello" << std::endl;
daniel << "How " << "are you";
daniel << " doing?" << std::endl;
// We are ready to process the request so do it
daniel.process();
// And Daniel's answer is...
std::string part1;
std::string part2;
std::string part3;
// Will read "I'm"
daniel >> part1;
// Will read "fine,"
daniel >> part2;
// Will read "thanks!"
daniel >> part3;
std::cout << "Daniel says: " << part1 << " " << part2 << " " << part3 << std::endl;
return 0;
}
答案 1 :(得分:1)
rdbuf()不返回字符串。你应该调用str()从stringstream获取一个std :: string。您需要将这两个函数声明为类的友元,以便访问其私有属性。
class Person
{
public:
void process();
friend std::ostream& operator<<(std::ostream& o, const person& p);
friend std::istream& operator>>(std::istream& i, person& p);
private:
std::stringstream _request;
std::stringstream _response;
}
std::ostream& operator<<(std::ostream& o, const person& p)
{
o << p._response.str();
return (o);
}
std::istream& operator>>(std::istream& i, person& p)
{
i >> p._request;
// internal process() call probably will be better
return i;
}