virtual关键字很重要,它保证多态正常调用。下面例子Derived的getI方法并没有调用,而是调用了父类Base的getI方法,并且delete对象时也没有调用Derived的析构函数,这不是我们期望的。问题原因正上因为没有使用virtual关键字。Parent版本才是我们期望的正常结果。
纯虚函数是把Parent的getI方法赋值0,表明是抽象类,不能构造实例。
virtual int getI() = 0;
虚函数保证多态正常调用的原理,不属于本文章内容。本文的目的只是说明问题和虚函数的作用,调用原理将会新写一篇文章详细说明。
#include <iostream>
using namespace std;
#define LOG(x) cout << x << endl;
class Base {
int i;
public:
Base(int j):i(j)
{
LOG("Base");
}
~Base(void)
{
LOG("~Base");
}
int getI()
{
LOG("Base::getI() is called");
return i;
}
};
class Derived : public Base {
int i;
public:
Derived(int j):Base(j) {
LOG("Derived");
}
~Derived(void)
{
LOG("~Derived");
}
int getI() {
LOG("Derived::getI() is called");
return i;
}
};
class Parent {
int i;
public:
Parent(int j):i(j)
{
LOG("Parent");
}
virtual ~Parent(void)
{
LOG("~Parent");
}
virtual int getI()
{
LOG("Parent::getI() is called");
return i;
}
};
class Child : public Parent {
int i;
public:
Child(int j):Parent(j) {
LOG("Child");
}
~Child(void)
{
LOG("~Child");
}
int getI() {
LOG("Child::getI() is called");
return i;
}
};
void main() {
Base* a = new Derived(5);
a->getI();
delete a;
Parent* b = new Child(8);
b->getI();
delete b;
getchar();
/* output
Base
Derived
Base::getI() is called
~Base
Parent
Child
Child::getI() is called
~Child
~Parent
*/
}