ostream& as parameter -> segfault? / understanding polymorphia

H

Henning Hasemann

My class looks very exactly like this (left out boring things):

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

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

std::eek:stream& operator<<(std::eek: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
 
D

doug turnbull

Henning said:
My class looks very exactly like this (left out boring things):

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

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

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

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?
 
H

Henning Hasemann

doug said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,698
Latest member
LydiaHalle

Latest Threads

Top