H
Henning Hasemann
My class looks very exactly like this (left out boring things):
class Situation {
public:
// ...
virtual void toStream(std:stream& ostr) const;
};
std::string Situation::toString() const {
std:stringstream oss;
toStream(oss);
return oss.str();
}
std:stream& operator<<(std:stream& ostr, const Situation& s) {
s.toStream(ostr); // segfault here
return ostr;
}
The toStream()-Method shall describe the class instance by writing some
things to the given stream.
Additionally I have lots of classes which derive from Situation and have
their own version of toStream().
The idea now is I have a set of References to these dervied classes (but
I hold them in a Situation& as I dont know the exact class of each one),
and I'd like to output them to cout or whatever.
But when I do I get a segfault on calling toStream()?
Why?
As the second parameter to oeprator<< is a reference I should be able to
take advantage of polymorphic behaviour, or not?
TIA
Henning
class Situation {
public:
// ...
virtual void toStream(std:stream& ostr) const;
};
std::string Situation::toString() const {
std:stringstream oss;
toStream(oss);
return oss.str();
}
std:stream& operator<<(std:stream& ostr, const Situation& s) {
s.toStream(ostr); // segfault here
return ostr;
}
The toStream()-Method shall describe the class instance by writing some
things to the given stream.
Additionally I have lots of classes which derive from Situation and have
their own version of toStream().
The idea now is I have a set of References to these dervied classes (but
I hold them in a Situation& as I dont know the exact class of each one),
and I'd like to output them to cout or whatever.
But when I do I get a segfault on calling toStream()?
Why?
As the second parameter to oeprator<< is a reference I should be able to
take advantage of polymorphic behaviour, or not?
TIA
Henning