本文学习自 狄泰软件学院 唐佐林老师的 C++课程
实验1 : 对象的本质,运行时对象退化为普通的结构体,权限访问关键字在程序运行时失效,可以通过内存地址直接访问成员变量。
实验2:对象的本质,运行时对象退化为普通的结构体,对象在内存上所包含的就只有成员变量
实验3:通过C语言 模拟类的函数调用!!!
成员变量可能存在 栈空间,堆空间,全局数据区。而 成员函数只能在代码段。
实验1 : 对象的本质,运行时对象退化为普通的结构体,权限访问关键字在程序运行时失效,可以通过内存地址直接访问成员变量。
#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(A) = " << sizeof(A) << endl; // 20 bytes
cout << "sizeof(a) = " << sizeof(a) << endl; // 20 bytes
cout << "sizeof(B) = " << sizeof(B) << endl; // 20 bytes
a.print();
B* p = reinterpret_cast<B*>(&a);
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;
}
mhr@ubuntu:~/work/c++$
mhr@ubuntu:~/work/c++$ g++ 50-1.cpp
mhr@ubuntu:~/work/c++$ ./a.out
sizeof(A) = 24
sizeof(a) = 24
sizeof(B) = 24
i = 4197504, j = 0, c = , d = 6.953e-310
i = 1, j = 2, c = c, d = 3
i = 100, j = 200, c = C, d = 3.14
mhr@ubuntu:~/work/c++$
说明:一个对象本质上就是一个特殊的结构体,由于A中的成员变量在内存空间中的排布与B在内存空间中的排布是一样的,所以可以通过如下方式 修改A 的成员变量的值
其实就和普通的数据类型转换思想是一样的,类型只是模子,不必在意模子的名称是不是一样,模子一样就可以了。
B* p = reinterpret_cast<B*>(&a);
p->i = 1;
p->j = 2;
p->c = 'c';
p->d = 3;
我们可以通过指针,类型转换,直接通过内存地址访问内存变量。类里面的访问权限关键字在运行的时候就失效了,如 private成员不能在类的外部直接访问,这一点仅仅是在编译的时候有效,也就是编译器进行语法检查的时候访问权限关键字是有效的,但是一旦编译通过,C++面向对象程序将变成二进制可执行程序,这时候就没有访问权限关键字的概念了,我们依旧可以通过指针,内存地址 直接修改成员变量的值,就如上例,定义一个内存排布一样的结构体或类B,通过 B* p = reinterpret_cast<B*>(&a); 指向A内存空间,直接访问A 的成员变量。
this 指针,代码段的成员函数通过隐藏的 this 指针(对象的地址,即结构体的地址) 来直接访问对象中(结构体中的)的成员变量。
实验2:对象的本质,运行时对象退化为普通的结构体,对象在内存上所包含的就只有成员变量
#include <iostream>
#include <string>
using namespace std;
class Demo
{
int mi;
int mj;
public:
Demo(int i, int j)
{
mi = i;
mj = j;
}
int getI()
{
return mi;
}
int getJ()
{
return mj;
}
int add(int value)
{
return mi + mj + value;
}
};
int main()
{
Demo d(1, 2);
cout << "sizeof(d) = " << sizeof(d) << endl;
cout << "d.getI() = " << d.getI() << endl;
cout << "d.getJ() = " << d.getJ() << endl;
cout << "d.add(3) = " << d.add(3) << endl;
return 0;
}
mhr@ubuntu:~/work/c++$ g++ 50-2.cpp
mhr@ubuntu:~/work/c++$ ./a.out
sizeof(d) = 8
d.getI() = 1
d.getJ() = 2
d.add(3) = 6
mhr@ubuntu:~/work/c++$
实验3:通过C语言 模拟类的函数调用!!!
50-2.h
#ifndef _50_2_H_
#define _50_2_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
50-2.c
#include "50-2.h"
#include "malloc.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);
}
main.c
#include <stdio.h>
#include "50-2.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;
Demo_Free(d);
return 0;
}
mhr@ubuntu:~/work/c++$
mhr@ubuntu:~/work/c++$ gcc 50-2.c main.c
mhr@ubuntu:~/work/c++$ ./a.out
d.mi = 1
d.mj = 2
Add(3) = 6
mhr@ubuntu:~/work/c++$