C++多态与虚函数条件

看下边代码:

#include<iostream>

class Base
{
public:
	Base(int a):ma(a){}
	void Show()
	{
		std::cout << "ma:" << ma << std::endl;
	}
protected:
	int ma;
};
class Derive :public Base
{
public:
	Derive(int b):mb(b),Base(b){}
	void Show()
	{
		std::cout << "mb:" << mb << std::endl;
	}
protected:
	int mb;
};
int main()
{
	Base* pb = new Derive(10);
	std::cout << sizeof(Base) << std::endl;
	std::cout << sizeof(Derive) << std::endl;
	std::cout << typeid(pb).name() << std::endl;
	std::cout << typeid(*pb).name() << std::endl;
	pb->Show();
	return 0;
}

运行结果:
在这里插入图片描述

Base* pb = new Derive(10);
pb->Show();

Base* pb = new Derive(10)
基类指针指向派生类对象
指向的是派生类对象中基类的起始部分
派生类对象布局:
在这里插入图片描述
动多态的发生:
在这里插入图片描述
动多态的处理流程:
基类指针找派生类中虚函数指针,然后派生类中的虚函数指针解引用到派生类虚表中的派生类函数
在这里插入图片描述
所以在基类方法中加上virtual关键字时,基类中增加了虚函数,也就存在了虚表,用虚函数指针访问虚表所以多了4个字节,派生类中多四个字节是因为派生类中也存在虚函数Show(),同理会增加四个字节的空间,pb为指针类型,内置类型只和定义点相关,所以无论何时都是Base*类型,对pb进行解引用为派生类的虚函数表,表中放的是派生类对象的类型信息,所以打印结果为class Derive,同时也调用派生类的方法Derive::Show();

那些函数可以成为虚函数:
1,构造函数 F
2,析构函数 T
3,inline函数 F
4,static修饰函数 thiscall ->cdcall F
5,全局函数 F
6,普通类成员方法 T

虚函数条件:
1,能取地址 inline函数PASS
2,依赖对象调用

所以只有析构函数和普通类成员方法可以成为虚函数,其他的都不可以。

猜你喜欢

转载自blog.csdn.net/Gunanhuai/article/details/104260335