Coder's Guild Mailing List

Re: Frank Hale: Virtual Functions continued...

Posted by Kspansel@xxx.xxx on 1999-09-13

> But in your example, the method show() is defined as virtual in the parent
>  class. And as the documentation states you don't have to add the virtual
>  keyword in the childclass. If you add the virtual keyword to some method it
>  will be a virtual method in all it's childclasses.


What I understood with the virtual functions is that if you have some thing 
like this:


class Test
{
    public:
        int SomeFunction();
};

And you derive a child class from this with the same function:

class ChildTest : public Test
{
    public:
        int SomeFunction();
}

You can call it fine when you do it this way:

ChildTest ct;
int ret = ct.SomeFunction();        //calls ChildTest::SomeFunction()

But you make it a pointer then like so:

ChildTest *ct;
int ret = ct->SomeFunction();   //calls Test::SomeFunction();

Then it will call the parent classes function...unless you make it virtual 
then it will always call the derived class's function.  I think someone may 
have already said this..but this is how I interpereted what I have learned.  
Laters

Kory Spansel