Tuesday, January 18, 2011

Virtual functions

In the previous lesson on pointers and references to the base class of derived objects, we took a look at a number of examples where using pointers or references to a base class had the potential to simplify code. However, in every case, we ran up against the problem that the base pointer or reference was only able to call the base version of a function, not a derived version.
Here’s a simple example of this behavior:
01class Base
02{
03protected:
04 
05public:
06    const char* GetName() { return "Base"; }
07};
08 
09class Derived: public Base
10{
11public:
12    const char* GetName() { return "Derived"; }
13};
14 
15int main()
16{
17    Derived cDerived;
18    Base &rBase = cDerived;
19    cout << "rBase is a " << rBase.GetName() << endl;
20}
This example prints the result:
rBase is a Base
Because rBase is a Base pointer, it calls Base::GetName(), even though it’s actually pointing to the Base portion of a Derived object.
In this lesson, we will address this issue using virtual functions.
Virtual functions
A virtual function is a special type of function that resolves to the most-derived version of the function with the same signature. To make a function virtual, simply place the “virtual” keyword before the function declaration.
Note that virtual functions and virtual base classes are two entirely different concepts, even though they share the same keyword.
Here’s the above example with a virtual function:
01class Base
02{
03protected:
04 
05public:
06    virtual const char* GetName() { return "Base"; }
07};
08 
09class Derived: public Base
10{
11public:
12    virtual const char* GetName() { return "Derived"; }
13};
14 
15int main()
16{
17    Derived cDerived;
18    Base &rBase = &cDerived;
19    cout << "rBase is a " << rBase.GetName() << endl;
20 
21    return 0;
22}
This example prints the result:
rBase is a Derived
Because rBase is a pointer to the Base portion of a Derived object, when rBase.GetName() is evaluated, it would normally resolve to Base::GetName(). However, Base::GetName() is virtual, which tells the program to go look and see if there are any more-derived versions of the function available. Because the Base object that rBase is pointing to is actually part of a Derived object, the program will check every inherited class between Base and Derived and use the most-derived version of the function that it finds. In this case, that is Derived::GetName()!
Let’s take a look at a slightly more complex example:
01class A
02{
03public:
04    virtual const char* GetName() { return "A"; }
05};
06 
07class B: public A
08{
09public:
10    virtual const char* GetName() { return "B"; }
11};
12 
13class C: public B
14{
15public:
16    virtual const char* GetName() { return "C"; }
17};
18 
19class D: public C
20{
21public:
22    virtual const char* GetName() { return "D"; }
23};
24 
25int main()
26{
27    C cClass;
28    A &rBase = cClass;
29    cout << "rBase is a " << rBase.GetName() << endl;
30 
31    return 0;
32}
What do you think this program will output?
Let’s look at how this works. First, we instantiate a C class object. rBase is an A pointer, which we set to point to the A portion of the C object. Finally, we call rBase.GetName(). rBase.GetName() evaluates to A::GetName(). However, A::GetName() is virtual, so the compiler will check all the classes between A and C to see if it can find a more-derived match. First, it checks B::GetName(), and finds a match. Then it checks C::GetName() and finds a better match. It does not check D::GetName() because our original object was a C, not a D. Consequently, rBase.GetName() resolves to C::GetName().
As a result, our program outputs:
rBase is a C
A more complex example
Let’s take another look at the Animal example we were working with in the previous lesson. Here’s the original class:
01#include <string>
02class Animal
03{
04protected:
05    std::string m_strName;
06 
07    // We're making this constructor protected because
08    // we don't want people creating Animal objects directly,
09    // but we still want derived classes to be able to use it.
10    Animal(std::string strName)
11        : m_strName(strName)
12    {
13    }
14 
15public:
16    std::string GetName() { return m_strName; }
17    const char* Speak() { return "???"; }
18};
19 
20class Cat: public Animal
21{
22public:
23    Cat(std::string strName)
24        : Animal(strName)
25    {
26    }
27 
28    const char* Speak() { return "Meow"; }
29};
30 
31class Dog: public Animal
32{
33public:
34    Dog(std::string strName)
35        : Animal(strName)
36    {
37    }
38 
39    const char* Speak() { return "Woof"; }
40};
And here’s the class with virtual functions:
01#include <string>
02class Animal
03{
04protected:
05    std::string m_strName;
06 
07    // We're making this constructor protected because
08    // we don't want people creating Animal objects directly,
09    // but we still want derived classes to be able to use it.
10    Animal(std::string strName)
11        : m_strName(strName)
12    {
13    }
14 
15public:
16    std::string GetName() { return m_strName; }
17    virtual const char* Speak() { return "???"; }
18};
19 
20class Cat: public Animal
21{
22public:
23    Cat(std::string strName)
24        : Animal(strName)
25    {
26    }
27 
28    virtual const char* Speak() { return "Meow"; }
29};
30 
31class Dog: public Animal
32{
33public:
34    Dog(std::string strName)
35        : Animal(strName)
36    {
37    }
38 
39    virtual const char* Speak() { return "Woof"; }
40};
Note that we didn’t make Animal::GetName() virtual. This is because GetName() is never overridden in any of the derived classes, therefore there is no need.
Now, using the virtual Speak() function, the following function should work correctly:
01void Report(Animal &rAnimal)
02{
03    cout << rAnimal.GetName() << " says " << rAnimal.Speak() << endl;
04}
05 
06int main()
07{
08    Cat cCat("Fred");
09    Dog cDog("Garbo");
10 
11    Report(cCat);
12    Report(cDog);
13}
Indeed, this program produces the result:
Fred says Meow
Garbo says Woof
When cAnimal.Speak() is evaluated, the program notes that it is a virtual function. In the case where rAnimal is pointing to the Animal portion of a Cat object, the program looks at all the classes between Animal and Cat to see if it can find a more derived function. In that case, it finds Cat::Speak(). In the case where rAnimal points to the Animal portion of a Dog object, the program resolves the function call to Dog::Speak().
Similarly, the following array example now works as expected:
1Cat cFred("Fred"), cTyson("Tyson"), cZeke("Zeke");
2Dog cGarbo("Garbo"), cPooky("Pooky"), cTruffle("Truffle");
3 
4// Set up an array of pointers to animals, and set those pointers to our Cat and Dog objects
5Animal *apcAnimals[] = { &cFred, &cGarbo, &cPooky, &cTruffle, &cTyson, &cZeke };
6for (int iii=0; iii < 6; iii++)
7    cout << apcAnimals[iii]->GetName() << " says " << apcAnimals[iii]->Speak() << endl;
Which produces the result:
Fred says Meow
Garbo says Woof
Pooky says Woof
Truffle says Woof
Tyson says Meow
Zeke says Meow
Even though these two examples only use Cat and Dog, any other classes we derive from Animal would also work with our Report() function and Animal array without further modification! This is perhaps the biggest benefit of virtual functions — the ability to structure your code in such a way that newly derived classes will automatically work with the old code without modification!
A word of warning: the signature of the derived class function must exactly match the signature of the base class virtual function in order for the derived class function to be used. If the derived class function has different parameter types, the program will likely still compile fine, but the virtual function will not resolve as intended.
Use of the virtual keyword
Technically, the virtual keyword is not needed in derived class. For example:
01class Base
02{
03protected:
04 
05public:
06    virtual const char* GetName() { return "Base"; }
07};
08 
09class Derived: public Base
10{
11public:
12    const char* GetName() { return "Derived"; } // note lack of virtual keyword
13};
14 
15int main()
16{
17    Derived cDerived;
18    Base &rBase = cDerived;
19    cout << "rBase is a " << rBase.GetName() << endl;
20 
21    return 0;
22}
prints
rBase is a Derived
Exactly the same as if Derived::GetName() was explicitly tagged as virtual. Only the most base class function needs to be tagged as virtual for all of the derived functions to work virtually. However, having the keyword virtual on the derived functions does not hurt, and it serves as a useful reminder that the function is a virtual function rather than a normal one. Consequently, it’s generally a good idea to use the virtual keyword for virtualized functions in derived classes even though it’s not strictly necessary.
Return types of virtual functions
Under normal circumstances, the return type of a virtual function and it’s override must match. Thus, the following will not work:
01class Base
02{
03public:
04    virtual int GetValue() { return 5; }
05};
06 
07class Derived: public Base
08{
09public:
10    virtual double GetValue() { return 6.78; }
11};
However, there is one special case in which this is not true. If the return type of a virtual function is a pointer or a reference to a class, override functions can return a pointer or a reference to a derived class. These are called covariant return types. Here is an example:
01class Base
02{
03public:
04    // This version of GetThis() returns a pointer to a Base class
05    virtual Base* GetThis() { return this; }
06};
07 
08class Derived: public Base
09{
10    // Normally override functions have to return objects of the same type as the base function
11    // However, because Derived is derived from Base, it's okay to return Derived* instead of Base*
12    virtual Derived* GetThis() { return this; }
13};
Note that some older compilers (eg. Visual Studio 6) do not support covariant return types.

No comments: