Re: a problem with poliporphism
Nafai wrote:[color=blue]
> I am going to give a more detailed example:
>
> // I don't type the constructors.
>
> class Event {
> private:
> int time;
> EventType kind;
> public:
> enum EventType { Event1, Event2,...,Emer gency,Crime};
> int when() {return time; }
> EventType what() { return kind; }
> }
>
> class Emergency : public Event {
> private:
> int area;
> public:
> int where() {return area;}
> };
>
> class Crime : public Event {
> private:
> string person;
> int aCrime;.
> public:
> string who() { return person; }
> int whatHeDid() { return aCrime; }
> };
>
> void doSomethingWith Events(Event* pE)
> {
> switch(pE->kind) {
> case Event::Emergenc y : callAmbulance(p E->where(),pE->when());
> break;
> case Event::Crime: blame(pE->who(),pE->whatHeDid(), pE->when());
> break;
> case Event::Event1 : doSomethingRela tedToEvent1(pE->when()); break;
> ...
> }
> }
>
> int main() {
> list<Event*> lst;
> .... // insert somehow Events in lst
> Event* pEvt = lst.first();
> lst.pop_first() ;
> doSomethingWith Event(pEvt);
> ....
> }
>
>
> My question is: which is the best way to do that? Is it OK like before?[/color]
In case it's not an oversight on your part, I just want to point out
that you should add a virtual destructor to your Event class. Even if
it's an empty *noop* you should still add it.
KPB
Nafai wrote:[color=blue]
> I am going to give a more detailed example:
>
> // I don't type the constructors.
>
> class Event {
> private:
> int time;
> EventType kind;
> public:
> enum EventType { Event1, Event2,...,Emer gency,Crime};
> int when() {return time; }
> EventType what() { return kind; }
> }
>
> class Emergency : public Event {
> private:
> int area;
> public:
> int where() {return area;}
> };
>
> class Crime : public Event {
> private:
> string person;
> int aCrime;.
> public:
> string who() { return person; }
> int whatHeDid() { return aCrime; }
> };
>
> void doSomethingWith Events(Event* pE)
> {
> switch(pE->kind) {
> case Event::Emergenc y : callAmbulance(p E->where(),pE->when());
> break;
> case Event::Crime: blame(pE->who(),pE->whatHeDid(), pE->when());
> break;
> case Event::Event1 : doSomethingRela tedToEvent1(pE->when()); break;
> ...
> }
> }
>
> int main() {
> list<Event*> lst;
> .... // insert somehow Events in lst
> Event* pEvt = lst.first();
> lst.pop_first() ;
> doSomethingWith Event(pEvt);
> ....
> }
>
>
> My question is: which is the best way to do that? Is it OK like before?[/color]
In case it's not an oversight on your part, I just want to point out
that you should add a virtual destructor to your Event class. Even if
it's an empty *noop* you should still add it.
KPB
Comment