Hmm... I think you should try an upcasting since the Word class is a subclass of HTMLElement class. I'll will look your code later, I'm a bit busy right now.
I don't get any errors. It works as expected when I have single objects derived from HTMLElement, but when I have a LinkedList<HTMLElement>, only the HTMLElement versin of << gets called, ignoring the ones specific for the derived clases.
That's why I'm asking if I have to do some kind of casting... it would be cool to have the code figure out by itself what it function it should use.
I think I know what the problem is here... but let me make sure I have everything right.
1) You want to output stuff for HTMLElement and Word
2) You figured you could output via the << operator
3) You have a container of HTMLElement* (which may be of HTMLElements or Words or other derived types)
4) You want each type to print things in its own way.
Assuming I have all of this right so far....
This screams polymorphism. And unfortuantely you can't make the << operator virtual in this case. So instead, make a virtual Print function:
1 2 3 4 5 6 7 8
class HTMLElement:
{
public:
virtualvoid Print(std::ostream& out) const
{
out << "whatever";
}
//...
Put the same thing in Word. And instead of doing cout << *ptr; , you do ptr->Print(cout);
EDIT:
a nice perk to this approach is that you can overload << too, and make it fake virtual by having it call Print:
1 2 3 4 5 6 7
// you only have to write this for HTMLElement, and it will work as
// desired for all derived types
std::ostream& operator << (std::ostream& out, const HTMLElement& el)
{
el.Print(out);
return out;
}