C++ OOP思想随手笔记

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
#include <stdio.h>


/*
*CFront 前置声明
*虚函数指针类型定义
*/
struct CFront;
typedef void (*pVirtualFun)(CFront* this, int, int);

//虚表
typedef struct
{
	//一系列函数指针
	pVirtualFun v;
}vtb;

//类定义
typedef struct
{
	//唯一的虚表指针(不发生多态时被优化掉)
	vtb* pt;
	//一系列类成员
	int a;
	int b;
}CFront;


//构造函数定义
void Constructor(CFront* this, int a, int b) {
	this->a = a;
	this->b = b;
	return;
}

//虚函数定义
void VirtualFun(CFront* this, int va, int vb) {
	this->a = va;
	this->b = vb;
	return;
}

int main() {
	//创建虚表
	vtb VB = { VirtualFun };
	//类创建对象(相当于初始化阶段)
	CFront A = { &VB,0,0};
	//调用构造函数(赋值阶段)
	CFront* this = &A;
	Constructor(this,10,11);


	printf("%d\n", this->a);

	return 0;
}

#ifdef __cplusplus
}
#endif // __cplusplus

猜你喜欢

转载自blog.csdn.net/A_Pointer/article/details/108084096