Sorry for opening so many new threads. I ll try not to pollute the forum for the next few days with my questions
I want to do this:
This will use always the A::display. I want if the object is B to use the B::display. I understand that this in not possible the way I do it.Code:class A { string name; void display(){cout << name}; }; class B : public A { void display(){cout << name << "B"}; }; list<A> list; put objects of A and B class in list for (everything in the list) p->display();
What would be the best way to do so?
I thought of adding a flag in the A class, which is set on upon constructing a B class to 1, so that way I can do this:
Which should work. But then again I don't think this is possible if p is an iterator, eh?Code:for (everything in the list) if (flag) (*(B*)p).display() else p->display();