c++ : a baseclass, derived classes, a list of baseclass pointers, a problem.



Hello out there..
I have a problem and no idea how to fix it..
The language is c++.

I have a base class, for example:

class a{
public:
virtual void func1(void) { }
};

Then i have some derived classes, maybe c, d, e...
I overwrote the virtual function func1() in each derived class.

Now i created a list of pointers to insances of the baseclass
within stl.

list<a*>mylist;

This is used to store pointers of the instances from
classes c, d, e.
So I could use this in one list..

The only Problem is:
If i go trough the list in a for loop,
and execute the func1-
How can I execute the right function?
I use a pointer to the baseclass to loop trough
the list.

I think i should determine which class it really is,
so i can cast the pointer to the right type,
because now theres always the function of the baseclass
executed.

Is this idea right?
How can i find out, which class the instance is of,
from outside of the class?
Or how could i execute the right function on an
easy way?

it's a little bit tricky...
But can i do this without a huge workaround?

Thanks for now
.



Relevant Pages

  • Re: Best way to store postfix data?
    ... based-pointer table and the executable tree as a based pointer system, ... Based pointers allow the pain-free conversion from relative pointers to absolute ... with control logic in the same time as the old read one line, execute ... but there's some memory hog I've missed in there. ...
    (microsoft.public.vc.mfc)
  • Re: is it good, is it bad or plain eeevil ?
    ... > also players, monsters, even locations that you can enter. ... > a container, so objects can be easily nested. ... > player, simply changing some pointers. ... virtual void doAdd; ...
    (comp.lang.cpp)
  • Re: Best way to store postfix data?
    ... Look into based pointers. ... with control logic in the same time as the old read one line, execute ... but there's some memory hog I've missed in there. ... that seems infinitely preferable to trying to store them inline ...
    (microsoft.public.vc.mfc)
  • Re: RunModalLoop and destructor
    ... Implement the virtual void OnCancel(); and virtual void OnOK; ... thr framework when the call to the modal dialog returns. ... > the whole program i would like to call delete on these pointers. ... > Unfortunately DestroyWindow() isn't called when the dialog is ...
    (microsoft.public.vc.mfc)
  • Re: Making an ActiveX control out of an executable
    ... Microsoft MVP, MCSD ... >>be coded to execute standalone as well. ... >>message handling differs. ... Thanks for the pointers. ...
    (microsoft.public.vc.atl)