为什么要使用多态特性,虚函数,虚函数的原理-虚函数表

这里写目录标题

  • 项目需求
    • 实现多态:虚函数
      • 使用继承的虚函数表
        • 多重继承的虚函数表

项目需求

因为各种不确定原因,包括认为原因,ODU设备会自动的切换到其它类型的设备,而切换后的设备,和原设备有很多不同的地方。如何完美的实现这个切换呢?

解决方案:
使用多态。

聚会案例:
Demo.cpp

#include 
using namespace std;

class Father {
     
public:
	void play() {
     
		cout << "到KTV唱歌..." << endl;
	}
};

class Son :public Father {
     
public:
	void play() {
     
		cout << "一起打王者吧!" << endl;
	}
};

void party(Father **men, int n) {
     
	for (int i = 0; i<n; i++) {
     
		men[i]->play();
	}
}
int main(void) {
     
	Father father;
	Son son1, son2;
	Father* men[] = {
      &father, &son1, &son2 };

	party(men, sizeof(men) / sizeof(men[0]));

	system("pause");
	return 0;
}
#include 
using namespace std;

class Father {
     
public:
	virtual void play() {
     
		cout << "到KTV唱歌..." << endl;
	}
};

class Son :public Father {
     
public:
	virtual void play() {
     
		cout << "一起打王者吧!" << endl;
	}
};

void party(Father **men, int n) {
     
	for (int i = 0; i<n; i++) {
     
		men[i]->play();
	}
}
int main(void) {
     
	Father father;
	Son son1, son2;
	Father* men[] = {
      &father, &son1, &son2 };

	party(men, sizeof(men) / sizeof(men[0]));

	system("pause");
	return 0;
}

实现多态:虚函数

多态的本质:
形式上,使用统一的父类指针做一般性处理,
但是实际执行时,这个指针可能指向子类对象,
形式上,原本调用父类的方法,但是实际上会调用子类的同名方法。

【注意】
程序执行时,父类指针指向父类对象,或子类对象时,在形式上是无法分辨的!
只有通过多态机制,才能执行真正对应的方法。

基础:虚函数的使用
虚函数的定义:
在函数的返回类型之前使用virtual
只在成员函数的声明中添加virtual, 在成员函数的实现中不要加virtual

虚函数的继承:
如果某个成员函数被声明为虚函数,那么它的子类【派生类】,以及子类的子类中,所继承的这个成员函数,也自动是虚函数。
如果在子类中重写这个虚函数,可以不用再写virtual, 但是仍建议写virtual, 更可读!

进阶1:虚函数的原理-虚函数表

单个类的虚函数表

#include 
using namespace std;

class Father {
     
public:
	virtual void func1() {
      cout << "Father::func1" << endl; }
	virtual void func2() {
      cout << "Father::func2" << endl; }
	virtual void func3() {
      cout << "Father::func3" << endl; }
	void func4() {
      cout << "非虚函数:Father::func4" << endl; }
public:  //为了便于测试,特别该用public
	int x = 100;
	int y = 200;
	static int z;
};

typedef void (*func_t)(void);
int Father::z = 1;
int main(void) {
     
	Father father;

	// 含有虚函数的对象的内存中,最先存储的就是“虚函数表”
	cout << "对象地址:" << (int*)&father << endl;

	int* vptr = (int*)*(int*)&father;
	cout << "虚函数表指针vptr:" << vptr << endl;

	cout << "调用第1个虚函数: ";
	((func_t) * (vptr + 0))();

	cout << "调用第2个虚函数:";
	((func_t) * (vptr + 1))();

	cout << "调用第3个虚函数: ";
	((func_t) * (vptr + 2))();

	
	cout << "第1个数据成员的地址: " << endl;
	cout <<  &father.x << endl;
	cout << std::hex << (int)&father + 4 << endl;
	cout << "第1个数据成员的值:" << endl;
	cout << std::dec <<  father.x << endl;
	cout << *(int*)((int)&father + 4) << endl;

	cout << "第2个数据成员的地址: " << endl;
	cout << &father.y << endl;
	cout << std::hex << (int)&father + 8 << endl;
	cout << "第2个数据成员的值:" << endl;
	cout << std::dec << father.y << endl;
	cout << *(int*)((int)&father + 8) << endl;

	cout << "sizeof(father)==" << sizeof(father) << endl;

	Father father2;
	cout << "father的虚函数表:";
	cout << *(int*)(*(int*)&father) << endl;
	cout << "father2的虚函数表:";
	cout << *(int*)(*(int*)&father2) << endl;

	system("pause");
	return 0;
}

为什么要使用多态特性,虚函数,虚函数的原理-虚函数表_第1张图片
为什么要使用多态特性,虚函数,虚函数的原理-虚函数表_第2张图片

为什么要使用多态特性,虚函数,虚函数的原理-虚函数表_第3张图片
对象内,首先存储的是“虚函数表指针”,又称“虚表指针”。
然后再存储非静态数据成员。

对象的非虚函数,保存在类的代码中!
对象的内存,只存储虚函数表和数据成员
(类的静态数据成员,保存在数据区中,和对象是分开存储的)

添加虚函数后,对象的内存空间不变!仅虚函数表中添加条目
多个对象,共享同一个虚函数表!

使用继承的虚函数表

Demo.cpp

#include 
using namespace std;

class Father {
     
public:
	virtual void func1() {
      cout << "Father::func1" << endl; }
	virtual void func2() {
      cout << "Father::func2" << endl; }
	virtual void func3() {
      cout << "Father::func3" << endl; }
	void func4() {
      cout << "非虚函数:Father::func4" << endl; }
public:  //为了便于测试,特别该用public
	int x = 100;
	int y = 200;
};

class Son : public Father {
     
public:
	void func1() {
      cout << "Son::func1" << endl; }
	virtual void func5() {
      cout << "Son::func5" << endl; }
};

typedef void (*func_t)(void);

int main(void) {
     
	Father father;
	Son  son;

	// 含有虚函数的对象的内存中,最先存储的就是“虚函数表”
	cout << "son对象地址:" << (int*)&son << endl;

	int* vptr = (int*)*(int*)&son;
	cout << "虚函数表指针vptr:" << vptr << endl;

	for (int i = 0; i < 4; i++) {
     
		cout << "调用第" << i + 1 << "个虚函数:";
		((func_t) * (vptr + i))();
	}
	
	for (int i = 0; i < 2; i++) {
     
		// +4 是因为先存储了虚表指针
		cout << *(int*)((int)&son + 4 + i * 4) << endl;
	}

	system("pause");
	return 0;
}

为什么要使用多态特性,虚函数,虚函数的原理-虚函数表_第4张图片
为什么要使用多态特性,虚函数,虚函数的原理-虚函数表_第5张图片
为什么要使用多态特性,虚函数,虚函数的原理-虚函数表_第6张图片

多重继承的虚函数表

#include 

using namespace std;

class Father {
     
public:
	virtual void func1() {
      cout << "Father::func1" << endl; }
	virtual void func2() {
      cout << "Father::func2" << endl; }
	virtual void func3() {
      cout << "Father::func3" << endl; }
	void func4() {
      cout << "非虚函数:Father::func4" << endl; }
public:
	int x = 200;
	int y = 300;
	static int z;
};

class Mother {
     
public:
	virtual void handle1() {
      cout << "Mother::handle1" << endl; }
	virtual void handle2() {
      cout << "Mother::handle2" << endl; }
	virtual void handle3() {
      cout << "Mother::handle3" << endl; }
public: //为了便于测试,使用public权限
	int m = 400;
	int n = 500;
};

class Son : public Father, public Mother {
     
public:
	void func1() {
      cout << "Son::func1" << endl; }
	virtual void handle1() {
      cout << "Son::handle1" << endl; }
	virtual void func5() {
      cout << "Son::func5" << endl; }
};

int Father::z = 0;

typedef void(*func_t)(void);

int main(void) {
     
	Son son;
	int* vptr = (int*) * (int*)&son;
	cout << "第一个虚函数表指针:" << vptr << endl;

	for (int i = 0; i < 4; i++) {
     
		cout << "调用第" << i + 1 << "个虚函数:";
		((func_t) * (vptr + i))();
	}

	for (int i = 0; i < 2; i++) {
     
		cout << *(int*)((int)&son + 4 + i * 4) << endl;
	}

	int* vptr2 = (int*) * ((int*)&son + 3);
	for (int i = 0; i < 3; i++) {
     
		cout << "调用第" << i + 1 << "个虚函数:";
		((func_t) * (vptr2 + i))();
	}

	for (int i = 0; i < 2; i++) {
     
		cout << *(int*)((int)&son + 16 + i * 4) << endl;
	}

	system("pause");
	return 0;
}

为什么要使用多态特性,虚函数,虚函数的原理-虚函数表_第7张图片
为什么要使用多态特性,虚函数,虚函数的原理-虚函数表_第8张图片
为什么要使用多态特性,虚函数,虚函数的原理-虚函数表_第9张图片

你可能感兴趣的:(笔记,C++,多态,指针,c++,链表)