C++笔记 第五十课 C++对象模型分析(上)---狄泰学院

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_42187898/article/details/84639653

如果在阅读过程中发现有错误,望评论指正,希望大家一起学习,一起进步。
学习C++编译环境:Linux

第五十课 C++对象模型分析(上)

1.回归本质

class是一种特殊的struct
在内存中class依旧可以看做变量的集合
class与struct遵循相同的内存对齐规则
class中的成员函数与成员变量是分开存放的
每个对象由独立的成员变量
所有对象共享类中的成员函数
值得思考的问题
在这里插入图片描述

50-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;//4+4+4+8 20bytes
    cout << "sizeof(a) = " << sizeof(a) << endl;  
    cout << "sizeof(B) = " << sizeof(B) << endl;//4+4+4+8 20bytes
    a.print();
    B* p = reinterpret_cast<B*>(&a);
    p->i = 100;
    p->j = 200;
    p->c = 'C';
    p->d = 3.14;
    a.print();
    return 0;
}
运行结果
sizeof(A) = 24
sizeof(a) = 24
sizeof(B) = 24
i = 4197456,j = 0,c =  ,d = 6.95323e-310
i = 100,j = 200,c = C,d = 3.14

2.C++对象模型分析

运行时的对象退化为结构体的形式
所有成员变量在内存中依次排布
成员变量间可能存在内存空隙
可以通过内存地址直接访问成员变量
访问权限关键字在运行时失效
类中的成员函数位于代码段中
调用成员函数时对象地址作为参数隐式传递
成员函数通过对象地址访问成员变量
C++语法规则隐藏了对象地址的传递过程

50-2 对象本质分析

面向对象不是C++专有的,依然可以用C语言来写
C语言中,对于一个类而言,成员函数和成员变量在内存里面是分开存放的,这一点非常重要,如果掌握了这些本质,就可以编写任何面向对象的代码

C++程序: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; //8 bytes
    cout << "d.getI() = " << d.getI() << endl;   //1
    cout << "d.getJ() = " << d.getJ() << endl;   //2
    cout << "d.add(3) = " << d.add(3) << endl;   //6
    
    return 0;
}

C语言中的程序:50-2.h ,50-2.c, main.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 "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);
}
运行结果
d.mi = 1
d.mj = 2
Add(3) = 6

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

猜你喜欢

转载自blog.csdn.net/weixin_42187898/article/details/84639653