---- 整理自狄泰软件唐佐林老师课程
#include
#include
using namespace std;
class A
{
int i; // 4
int j; // 4
char c; // 1 --> 8
double d; // 8
public:
void print() {
cout << "i = " << i << ", " << "j = " << j << ", "
<< "c = " << c << ", " << "d = " << d << endl;
}
};
struct B
{
int i; // 4
int j; // 4
char c; // 1 --> 8
double d; // 8
};
int main()
{
A a;
cout << "sizeof(A) = " << sizeof(A) << endl;
cout << "sizeof(a) = " << sizeof(a) << endl;
cout << "sizeof(B) = " << sizeof(B) << endl;
a.print();
B* p = reinterpret_cast<B*>(&a); // 强制类型转换,重解释 A 这一段内存
// struct 解释 class
p->i = 1;
p->j = 2;
p->c = 'c';
p->d = 3;
a.print();
p->i = 100;
p->j = 200;
p->c = 'C';
p->d = 3.14;
a.print();
return 0;
}
#include
#include
using namespace std;
class Demo
{
int mi;
int mj;
public:
Demo(int i, int j) {
mi = i;
mj = j;
}
int getI() {
cout << "this: " << this << endl;
return mi;
}
int getJ() {
cout << "this: " << this << endl;
return mj;
}
int add(int value) {
cout << "this: " << this << endl;
return mi + mj + value;
}
};
int main()
{
Demo d(1, 2);
cout << "sizeof(d) = " << sizeof(d) << endl;
cout << "&d = " << &d << endl;
cout << "d.getI() = " << d.getI() << endl; // d 对象的地址被传入到成员函数内部
cout << "d.getJ() = " << d.getJ() << endl;
cout << "d.add(3) = " << d.add(3) << endl;
return 0;
}
#ifndef __DEMO_H_
#define __DEMO_H_
typedef void Demo;
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);
#endif
#include
#include "demo.h"
struct ClassDemo {
int mi;
int mj;
};
Demo* Demo_create(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);
}
#include
#include "demo.h"
int main()
{
Demo* d = Demo_create(1, 2); // 模拟 Demo* d = new Demo(1,2);
printf("d.mi = %d\n", Demo_GetI(d)); // 模拟 d->GetI();
printf("d.mj = %d\n", Demo_GetJ(d)); // 模拟 d->GetJ();
printf("Add(3) = %d\n", Demo_Add(d, 3)); // 模拟 d->Add(3);
// d->mi = 100; // 编译报错,模拟 C++ 外界不能访问私有成员
Demo_Free(d); // 模拟 C++ 中的析构
return 0;
}
#include
#include
using namespace std;
class Demo
{
public:
int mi;
int mj;
};
class Derived : public Demo
{
public:
int mk;
};
int main()
{
Derived d;
cout << "&d.mi = " << &d.mi << endl;
cout << "&d.mj = " << &d.mj << endl;
cout << "&d.mk = " << &d.mk << endl;
cout << "sizeof(Demo) = " << sizeof(Demo) << endl;
cout << "sizeof(Derived) = " << sizeof(Derived) << endl;
return 0;
}
#include
#include
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 << ", "
<< "mk = " << mk << endl;
}
};
struct Test {
int mi;
int mj;
int mk;
};
int main()
{
cout << "sizeof(Demo) = " << sizeof(Demo) << endl;
cout << "sizeof(Derived) = " << sizeof(Derived) << endl;
Derived d(1, 2, 3);
Test* p = reinterpret_cast<Test*>(&d);
d.print();
cout << "Before changing ..." << endl;
p->mi = 10;
p->mj = 20;
p->mk = 30;
cout << "After changing ..." << endl;
d.print();
return 0;
}
#include
#include
using namespace std;
class Demo
{
protected:
int mi;
int mj;
public:
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 << ", "
<< "mk = " << mk << endl;
}
};
int main()
{
cout << "sizeof(void*) = " << sizeof(void*) << endl;
cout << "sizeof(Demo) = " << sizeof(Demo) << endl;
cout << "sizeof(Derived) = " << sizeof(Derived) << endl;
return 0;
}
#include
#include
using namespace std;
class Demo
{
protected:
int mi;
int mj;
public:
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 << ", "
<< "mk = " << mk << endl;
}
};
struct Test {
void* p;
int mi;
int mj;
int mk;
};
int main()
{
cout << "sizeof(void*) = " << sizeof(void*) << endl;
cout << "sizeof(Demo) = " << sizeof(Demo) << endl;
cout << "sizeof(Derived) = " << sizeof(Derived) << endl;
Derived d(1, 2, 3);
Test* p = reinterpret_cast<Test*>(&d);
d.print();
cout << "Before changing ..." << endl;
p->mi = 10;
p->mj = 20;
p->mk = 30;
cout << "After changing ..." << endl;
d.print();
return 0;
}
#ifndef __DEMO_H_
#define __DEMO_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_Create(int i, int j, int k);
int Derived_GetK(Derived* pThis);
int Derived_Add(Derived* pThis, int value);
#endif
#include
#include "demo.h"
static int Demo_Virtual_add(Demo* pThis, int value);
static int Derived_Virtual_add(Demo* pThis, int value);
struct VTable { // 2. 定义虚函数表数据结构
int (*pAdd)(void*, 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 = {
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_Create(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
#include "demo.h"
void run(Demo* p, int v)
{
int r = Demo_Add(p, v);
printf("r = %d\n", r);
}
int main()
{
Demo* pb = Demo_Create(1, 2);
Derived* pd = Derived_Create(10, 20, 30);
printf("pb->add(3) = %d\n", Demo_Add(pb, 3));
printf("pd->add(30) = %d\n", Derived_Add(pd, 30));
run(pb, 3);
run(pd, 30);
Demo_Free(pb);
Demo_Free(pd);
return 0;
}