虚函数——virtual

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
*/
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值