By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,332 Members | 1,505 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,332 IT Pros & Developers. It's quick & easy.

ostream& as parameter -> segfault? / understanding polymorphia

P: n/a
My class looks very exactly like this (left out boring things):

class Situation {
public:
// ...
virtual void toStream(std::ostream& ostr) const;
};

std::string Situation::toString() const {
std::ostringstream oss;
toStream(oss);
return oss.str();
}

std::ostream& operator<<(std::ostream& 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

Jun 23 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Henning Hasemann wrote:
My class looks very exactly like this (left out boring things):

class Situation {
public:
// ...
virtual void toStream(std::ostream& ostr) const;
};

std::string Situation::toString() const {
std::ostringstream oss;
toStream(oss);
return oss.str();
}

std::ostream& operator<<(std::ostream& 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


Maybe it has something to do with the relationship between virtual and
const. Does it crash on entry into the method or at a specific place
within the method?

Jun 24 '06 #2

P: n/a
doug turnbull wrote:

Maybe it has something to do with the relationship between virtual and
const. Does it crash on entry into the method or at a specific place
within the method?


I found the problem last night, it was the fault of that self-built
container (I made it too clever so it copied values if it got a const
one to insert, which made all const items to instances of their base
class only, addionally my copy-constructor was broken so I played around
with already deleted objects).

Thank you for your time anyway :-)

Henning
Jun 24 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.