I need to convert a ostream's object in a ostringstream's object. This is the code:
//gerarchia_view.h
class bagaglio_view{
private:
std::ostream& os;
std::string data;
public:
bagaglio_view();
std::string print_bagaglio(const bagaglio &b) const;
};
//gerarchia_view.cpp
bagaglio_view::bagaglio_view():data(" "),os(std::cout){
}
std::string bagaglio_view::print_bagaglio(const bagaglio& b) const{
os<<b; // operator << is overloaded
std::ostringstream* oss=static_cast<std::ostringstream*>(&os);
return oss->str(); // down-cast, segm fault
}
I need the function
std::string bagaglio_view::print_bagaglio(const bagaglio& b) const;
return the string that is in std::ostream os. How can I solve that? I tried dynamic_cast and using a std::ostringstream in substution of std::ostream, but operator<< doesn't work.