Tuesday, August 29, 2006

Virtual function in virtual base class

As you have declared the function to be const in base class but not in derived class, it is not overriding.

class Animal
{
public:
Animal() { age = 0 ;}
virtual int GetAge() const { return age; }

private:
int age;
};

class Eagle : virtual public Animal
{
public:
virtual int GetAge() { age = 4 ; return age; }

private:
int age ;
};

class Lion : virtual public Animal
{
public:
virtual int GetAge() { age = 10 ; return age; }

private:
int age ;
};

class Griffin : public Eagle ,public Lion
{
}


Also the virtual inheritance does not resolve the error for ambiguous functions. You need to override the function in the most derived (Griffin) class and call the appropriate base version.

No comments:

Post a Comment