类对象在内存布局、多态实现原理

class是一种特殊的struct

  • 在内存中class依旧可以看成是变量的集合
  • class和struct遵循相同的内存对齐原则
  • class的成员变量和成员函数是分开存放的
    • 每个对象有独立的成员变量,存栈、堆、全局数据区
    • 类所有对象共享类的成员函数,存代码段
#include <iostream>
#include <string>

using namespace std;

class A
{
	int i;
	int j;
	char c;
	double d;
public:
	void print()
	{
		cout << "i = " << i << ". "
			 << "j = " << j << ". "
			 << "c = " << c << ". "
			 << "d = " << d << ". " << endl;
	}
};

struct B
{
    int i;
    int j;
    char c;
    double d;
};

int main()
{
    A a;
    
	cout << "sizeof(B) = " << sizeof(B) << endl;    //24(64位机)  20(32位机)
    cout << "sizeof(A) = " << sizeof(A) << endl;    //24 
    cout << "sizeof(a) = " << sizeof(a) << endl;    //24
   
    a.print();
	
	B* p = reinterpret_cast<B*>(&a);
	
	p->i = 1;
	p->j = 2;
	p->c = 'c';
	p->d = 3;
	
	a.print();
    
    return 0;
}

运行时的对象退化为结构体的形式

  • 所有成员变量在内存中一次排布
  • 成员变量间可能存在内存空隙
  • 可通过内存地址直接访问成员变量
  • 访问权限关键字在运行时失效

类的成员函数

  • 类的成员函数位于代码段中
  • 调用成员函数时对象地址作为参数隐式传递
  • 成员函数通过对象地址访问成员变量
  • C++影藏对象地址的传递过程
#ifndef _50_2_H_
#define _50_2_H_

typedef void Demo;

Demo* Demo_Creat(int i, int j);
int Demo_GetI(Demo* pThis);
int Demo_GetJ(Demo* pThis);
int Demo_Add(Demo* pThis, int value);
void Demo_Free(Demo* pThis);

#endif
#include "50-2.h"
#include "malloc.h"

struct ClassDemo
{
	int mi;
	int mj;
};

Demo* Demo_Creat(int i, int j)
{
	struct ClassDemo* ret = (struct ClassDemo*)malloc(sizeof(struct ClassDemo));
	if(ret != NULL)
	{
		ret->mi = i;
		ret->mj = j;
	}
	
	return ret;
}
int Demo_GetI(Demo* pThis)
{
	struct ClassDemo* obj = (struct ClassDemo*)pThis;
	return obj->mi;
}
int Demo_GetJ(Demo* pThis)
{
	struct ClassDemo* obj = (struct ClassDemo*)pThis;
	return obj->mj;
}

int Demo_Add(Demo* pThis, int value)
{
	struct ClassDemo* obj = (struct ClassDemo*)pThis;
	return obj->mi + obj->mj + value;
}

void Demo_Free(Demo* pThis)
{
	free(pThis);
	pThis = NULL;
}



#include <stdio.h>
#include "50-2.h"

int main()
{
	Demo* d = Demo_Creat(1, 2);  // Demo* d = new Demo(1, 2);
	printf("d.mi = %d\n", Demo_GetI(d)); //d->getI();
	printf("d.mi = %d\n", Demo_GetJ(d)); //d->getJ();
	printf("d.mi = %d\n", Demo_Add(d, 3)); //d->add(3);
	
	//d->mi = 100;//Error
	
	Demo_Free(d);
	
	
    return 0;
}

小结

  • C++类对象在内存布局上与结构体相同
  • 成员变量和函数在内存中分开存放
  • 访问权限关键字在运行时失效
  • 调用成员参数时对象地址作为参数隐式传递

继承对象模型

  • 在C++编译器内部类可理解为结构体
  • 子类由父类成员叠加子类新成员得到
#include <iostream>
#include <string>

using namespace std;

class Demo
{
protected:	
	int mi;
	int mj;
	
};

class Derived : public Demo
{
	int mk;
public:
	Derived(int i, int j, int k)
	{
		mi = i;
		mj = j;
		mk = k;
	}
	void print()
	{
		cout << "mi = " << mi << "."
		     << "mj = " << mj << "."
			 << "mj = " << mk << "." << endl;
	}
	
};

struct Test
{
	int mi;
	int mj;
	int mk;
};

int main()
{
	cout << "sizeof(Demo) = " << sizeof(Demo) << endl;//8
	cout << "sizeof(Derived) = " << sizeof(Derived) << endl;//12
	
    Derived d(1, 2, 3);
	Test* p = reinterpret_cast<Test*>(&d);
	cout <<	"----before------"  << endl;
	d.print();
	
	p->mi = 10;
	p->mj = 20;
	p->mk = 30;
	
	cout <<	"---after-------"  << endl;
	d.print();
    return 0;
}

C++多态实现原理
  • 当类中申明虚函数时,编译器会在类中生成一个虚函数表
  • 虚函数表是一个存储成员函数地址的数据结构
  • 虚函数表是有编译器自动生成与维护的
  • virtual成员函数会被编译器放入虚函数表中
  • 存在虚函数时,每个对象中都有一个指向虚函数表的指针
#include <iostream>
#include <string>

using namespace std;

class Demo
{
protected:	
	int mi;
	int mj;
	virtual void print()
	{
		cout << "mi = " << mi << "."
		     << "mj = " << mj << "." << endl;
	}	
};

class Derived : public Demo
{
	int mk;
public:
	Derived(int i, int j, int k)
	{
		mi = i;
		mj = j;
		mk = k;
	}
	void print()
	{
		cout << "mi = " << mi << "."
		     << "mj = " << mj << "."
			 << "mj = " << mk << "." << endl;
	}
	
};

struct Test
{
	void* p;
	int mi;
	int mj;
	int mk;
};

int main()
{
	cout << "sizeof(Demo) = " << sizeof(Demo) << endl;//8
	cout << "sizeof(Derived) = " << sizeof(Derived) << endl;//12
	
    Derived d(1, 2, 3);
	Test* p = reinterpret_cast<Test*>(&d);
	cout <<	"----before------"  << endl;
	d.print();
	
	p->mi = 10;
	p->mj = 20;
	p->mk = 30;
	
	cout <<	"---after-------"  << endl;
	d.print();
	
	
    return 0;
}

C写面向对象的多态
#ifndef _51_2_H_
#define _51_2_H_

typedef void Demo;
typedef void Derived;


Demo* Demo_Create(int i, int j);
int Demo_GetI(Demo* pThis);
int Demo_GetJ(Demo* pThis);
int Demo_Add(Demo* pThis, int value);
void Demo_Free(Demo* pThis);

Derived* Derived_Creat(int i, int j, int k);
int Derived_GetK(Derived* pThis);
int Derived_Add(Derived* pThis, int value);

#endif
#include "51-2.h"
#include "malloc.h"

static int Demo_Virtual_Add(Demo* pThis, int value);//定义虚函数
static int Derived_Virtual_Add(Demo* pThis, int value);//定义虚函数

struct VTable	// 2.定义虚函数表数据结构
{
	int (*pAdd)(Derived*, int);	// 3.虚函数表里存储什么???
};

struct ClassDemo
{
	struct VTable* vptr;	//1.定义虚函数表指针==>虚函数表指针类型???
    int mi;
    int mj;
};

struct ClassDerived
{
	struct ClassDemo d;
	int mk;
};

static struct VTable g_Demo_vtbl = //虚函数表中放真正意义的add虚函数
{
	Demo_Virtual_Add
};

static struct VTable g_Derived_vtbl = 
{
	Derived_Virtual_Add
};


Demo* Demo_Create(int i, int j)
{
    struct ClassDemo* ret = (struct ClassDemo*)malloc(sizeof(struct ClassDemo)); 

    if( ret != NULL )
    {
		ret->vptr = &g_Demo_vtbl;	//4. 关联对象和虚函数表
        ret->mi = i;
        ret->mj = j;
    }
    
    return ret;
}

int Demo_GetI(Demo* pThis)
{
     struct ClassDemo* obj = (struct ClassDemo*)pThis;    

     return obj->mi;
}

int Demo_GetJ(Demo* pThis)
{
    struct ClassDemo* obj = (struct ClassDemo*)pThis;

    return obj->mj;
}

// 6.定义虚函数表中指针指向的具体函数
static int Demo_Virtual_Add(Demo* pThis, int value)
{
	struct ClassDemo* obj = (struct ClassDemo*)pThis;
	return obj->mi + obj->mj + value;
}

// 5.分析具体的虚函数!!!
int Demo_Add(Demo* pThis, int value)
{
	struct ClassDemo* obj = (struct ClassDemo*)pThis;
	return obj->vptr->pAdd(pThis, value);
}

void Demo_Free(Demo* pThis)
{
    free(pThis);
}

Derived* Derived_Creat(int i, int j, int k)
{
    struct ClassDerived* ret = (struct ClassDerived*)malloc(sizeof(struct ClassDerived)); 

    if( ret != NULL )
    {
		ret->d.vptr = &g_Derived_vtbl;
        ret->d.mi = i;
        ret->d.mj = j;
		ret->mk = k;
    }
    
    return ret;	
}
int Derived_GetK(Derived* pThis)
{
	struct ClassDerived* obj = (struct ClassDerived*)pThis;

    return obj->mk;
}

static int Derived_Virtual_Add(Demo* pThis, int value)
{
	struct ClassDerived* obj = (struct ClassDerived*)pThis;
	return obj->mk + value;	
}

int Derived_Add(Derived* pThis, int value)
{
	struct ClassDerived* obj = (struct ClassDerived*)pThis;
	return obj->d.vptr->pAdd(pThis, value);
}
#include "stdio.h"
#include "51-2.h"

void run(Demo* p, int v)
{
	int r = Demo_Add(p, 3);
	printf("r = %d\n", r);
}

int main()
{
	Demo* pb = Demo_Create(1, 2);
	Derived* pd = Derived_Creat(1, 22, 333);
	
	printf("pb->add(3) = %d\n", Demo_Add(pb, 3));
	printf("pd->add(3) = %d\n", Derived_Add(pd, 3));
	
	run(pb, 3);
	run(pd, 3);
	
	Demo_Free(pb);
	Demo_Free(pd);
	
 
    return 0;
}

小结

  • 继承本质是父子间成员变量的叠加
  • C++中多态是通过虚函数表实现的
  • 组函数表示编译器自动生成和维护
  • 虚函数的调用效率低于普通成员函数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值