Some notes about how virtual function used in C++. Which may called as polymorphism as well.
Let’s take a look on the following example code.
#include<iostream>
class Base {
public:
void func1() { std::cout << "Base func1 called" << std::endl; }
};
class Derived: public Base {
public:
void func1() { std::cout << "Derived func1 called" << std::endl; }
};
int main() {
Base b1;
b1.func1(); // Correct: Base func1 called
Derived d1;
d1.func1(); // Correct: Derived func1 called
Base *b2 = new Derived();
b2->func1(); // Wrong: Base func1 called
return 0;
}
What happen to b2? Why cannot we get the correct function be called. Cannot we reference to base class?
Keyword virtual
Why?
Due to func1() in Base class should declare as virtual function.
class Base {
public:
virtual void func1() { std::cout << "Base func1 called" << std::endl; }
};
class Derived: public Base {
public:
void func1() { std::cout << "Derived func1 called" << std::endl; }
};
Keyword override
override specifier (since C++11)
There is a keyword to make sure the derived class function override base class correctly called override.
class Base {
public:
void func1() {...} // Wrong way
// virtual void func1() {} // Correct way
};
class Derived: public Base {
public:
void func1() override {...} // Compile error
};
When you try to compile an override function without a virtual base function will get an error message said:
error: ‘void Derived::func1()’ marked ‘override’, but does not override. It can prevent miss-override.
Abstract Class and Pure Virtual Function
class Base { // abstract class
virtual void func1() = 0; // pure virtual function
};
class Derived: public base {
// Compile error
};
int main() {
Derived derived;
return 0;
}
The erroe message will show like cannot declare variable ‘derived’ to be of abstract type ‘Derived’ because the following virtual functions are pure within ‘A’: virtual void Base::func1().
Here are some rules:
- We cannot create objects from a abstract class (but okay to reference abstract base).
- Pure virtual function must be override in derived class.
Keyword final
final specifier (since C++11)
The function declare as function cannot be override anymore.
class Base {
virtual void foo();
};
class A: public Base {
void foo() final;
void bar() final; // Error: non-virtual cannot be final
};
class B: public A {
void foo() override; // Error: final funtion cannot be override
};
final specifier not only can use on function but also on class/struct.
While a class/struct is final, it cannot derived anymore.
Virtual Destructors
Virtual table(aka vtable) and vpointers are used to associate the virtual functions and the definition.
class Base {
public:
~Base() { std::cout << "Destroying base" << std::endl; }
};
class Derived: public Base {
public:
~Derived() { std::cout << "Destorying derived" << std::endl; }
};
int main(){
Base *b = new Derived();
delete b;
return 0;
}
We will only get Destroying base from the above code.
While the object be destructed, the destructor of derived object cannot be executed. Since the destructe function in Base class is not virtual.
class Base {
public:
virtual ~Base() { std::cout << "Destroying base" << std::endl; }
};
class Derived: public Base {
public:
~Derived() override { std::cout << "Destorying derived" << std::endl; }
};
int main(){
Base *b = new Derived();
delete b;
return 0;
}
We will get the following output with virtual deconstruct function.
Derived class deconstruct
Base class deconstruct