第50课 - C++ 对象模型分析(上)
2018-04-01 21:50
309 查看
1、回归本质
class是—种特殊的struct
-在内存中class依旧可以看作变量的集合
- class与struct遵循相同的内存对齐规则
- class中的成员函数与成员变量是分开存放的
每个对象有独立的成员变量
所有对象共享类中的成员函数
值得思考的问题
2、编程实验
对象内存布局初探 50-1.cpp
#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;
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-&g
18839
t;i = 100;
p->j = 200;
p->c = 'C';
p->d = 3.14;
a.print();
return 0;
}
3、C++对象模型分析
运行时的对象退化为结构体的形式
-所有成员变量在内存中依次排布
- 成员变量间可能存在内存空隙
-可以通过内存地址直接访问成员变量
-访问权限关键字在运行时失效
类中的成员函数位于代码段中
调用成员函数时对象地址作为参数隐式传递
成员函数通过对象地址访问成员变量
C++语法规则隐藏了对象地址的传递过程
4、编程实验
对象本质分析 50-2.cpp
#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;
}
下面用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-5.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; //实现封装 12:6: error: request for member ‘mi’ in something not a structure or union
Demo_Free(d);
return 0;
}
5、小结
C++中的类对象在内存布局上与结构体相同
成员变量和成员函数在内存中分开存放
访问权限关键字在运行时失效
调用成员函数时对象地址作为参数隐式传递
class是—种特殊的struct
-在内存中class依旧可以看作变量的集合
- class与struct遵循相同的内存对齐规则
- class中的成员函数与成员变量是分开存放的
每个对象有独立的成员变量
所有对象共享类中的成员函数
值得思考的问题
2、编程实验
对象内存布局初探 50-1.cpp
#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;
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-&g
18839
t;i = 100;
p->j = 200;
p->c = 'C';
p->d = 3.14;
a.print();
return 0;
}
3、C++对象模型分析
运行时的对象退化为结构体的形式
-所有成员变量在内存中依次排布
- 成员变量间可能存在内存空隙
-可以通过内存地址直接访问成员变量
-访问权限关键字在运行时失效
类中的成员函数位于代码段中
调用成员函数时对象地址作为参数隐式传递
成员函数通过对象地址访问成员变量
C++语法规则隐藏了对象地址的传递过程
4、编程实验
对象本质分析 50-2.cpp
#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;
}
下面用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-5.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; //实现封装 12:6: error: request for member ‘mi’ in something not a structure or union
Demo_Free(d);
return 0;
}
5、小结
C++中的类对象在内存布局上与结构体相同
成员变量和成员函数在内存中分开存放
访问权限关键字在运行时失效
调用成员函数时对象地址作为参数隐式传递
相关文章推荐
- 第50课 C++对象模型分析(上)
- 第50课-C++对象模型分析(上)
- C++多重继承和虚拟继承对象模型、效率分析
- C++多重继承和虚拟继承对象模型、效率分析
- c++对象内存模型分析工具
- C++对象模型和虚函数表分析以及重载、重写、隐藏的区别
- c++对象模型布局分析
- C++多重继承和虚拟继承对象模型、效率分析
- C++ 多重继承和虚拟继承对象模型、效率分析
- C++对象模型中的虚拟函数分析
- Microsoft Visual C++虚拟多继承 对象模型初步分析
- 第51课-C++对象模型分析(下)
- 29、不一样的C++系列--对象模型分析
- 【C++】菱形虚拟继承对象模型分析
- C++对象模型分析
- C++对象模型Data语意学分析、虚继承底层实现机制
- 第51课 C++对象模型分析(下)
- C++ - 多重继承和虚拟继承对象模型、效率分析
- 【C++对象模型】第六章 执行期语意学
- C++对象模型的那些事儿之四:拷贝构造函数