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



In article <43c58a1c$0$21033$9b4e6d93@xxxxxxxxxxxxxxxxxxxxxxxxxx>,
sid_kkr@xxxxxxxx says...
> 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.

That's the clever bit - you don't have to. Each instance of the
classes c, d and e contains a pointer to a thing called the vtable, of
which a separate one exists for c, d and e. The vtable in turn points
to the correct virtual function for each class.

So when the virtual function is called, the computer finds the correct
function for the class that is actually present.

> Is this idea right?
> How can i find out, which class the instance is of,
> from outside of the class?

As you see, it's not necessary. That's the whole point of virtual
functions!

You could cast to the base class and investigate the vtable pointer,
but there are probably better ways to do such things if you need to.

- Gerry Quinn
.



Relevant Pages

  • Re: Classes from pointers?
    ... you have a pointer to a base class which really points to a derived class, ... polymorphism is to call a virtual function which does the right thing no ... Consider using a reference counted smart pointer instead ...
    (comp.lang.cpp)
  • Re: 7.1 vs 8.0: Inheritance and Events
    ... pure virtual function in your base class that every derived class must ... Form the pointers to members from within the implementations of ... add_PanelActivated function rather than composing the pointer to member ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Pure Virtual Function Calls
    ... If the this pointer is invalid. ... the correct function will start to execute but the function may crash when data via the this pointer is accessed. ... A virtual function uses a vtable, so when calling a virtual function the this pointer must be correct because the code that executes a virtual function will fetch the address for the function at the proper index of the vtable. ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Base or deruved object?
    ... > If I have a base class and several possible derived classes, ... > a pointer to the base class, what's the best way of determining whether ... you should instroduce a virtual function and just call that ...
    (comp.lang.cpp)
  • executing a child method when pointed to by a base class pointer
    ... Is there a way of defining a method in a base class such that derived ... referred to by a pointer to the base class? ... 'child class' type). ... child class will execute the approriate method. ...
    (microsoft.public.dotnet.languages.vc)