在正常情况下,我们直接访问对象的私有变量,编译器会报错,那么有没有办法可以直接访问呢?有的,我们可以借助指针的方式去访问,类里面的成员是按顺序在内存里存放的,那么我们获得类的第一个成员的地址(即类对象的地址,这个可以自行验证),再根据各个成员变量的长度,即可获取每一个成员变量的地址。

PS:不能单纯考虑成员的实际类型,比如char,就认为它占1字节,需要考虑内存对齐的情况,我们可以借助命令:cl [filename].cpp /d1 reportSingleClassLayout[className]查看内存模型

TestA的内存模型:
TestA内存模型.png

TestB的内存模型:
TestB内存模型.png

程序代码:

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
using namespace std;

class TestA {
private:
    int m_a;
    char m_b;
    float m_c;
public:
    TestA(int a, char b, float c);
    void show();
};
TestA::TestA(int a, char b, float c) :m_a(a), m_b(b), m_c(c) {};

void TestA::show() {
    cout <<"通过public方法访问:"<<endl << m_a << "-" << m_b << "-" << m_c << endl;
}

class TestB {
private:
    int m_a;
    char m_b;
    double m_c;
    int m_d;
public:
    TestB(int a, char b, double c, int d);
    void show();
};
TestB::TestB(int a, char b, double c, int d) :m_a(a), m_b(b), m_c(c), m_d(d) {};
void TestB::show() {
    cout << "通过public方法访问:" << endl << m_a << "-" << m_b << "-" << m_c<<"-" << m_d << endl;
}

int main() {
    TestA testA(20, 'A', 40.5f);
    int x = *(int*)&testA;
    char y = *(char*)((char*)&testA + sizeof(int));
    float z = *(float*)((char*)&testA + sizeof(int) + sizeof(int));
    cout << "-------TEST A----"<<endl;
    testA.show();
    cout <<"外部通过内存地址访问类的private变量:"<<endl<< x << "-" << y << "-" << z << endl;

    TestB testB(10, 'B', 30.52,20);
    int x1 = *(int*)&testB;
    char y1 = *(char *) ((char *)&testB + sizeof(int));
    double z1 = *(double*)((char*)&testB + sizeof(int) + sizeof(int));
    int k1 = *(int*)((char*)&testB + sizeof(int) + sizeof(int)+sizeof(double));
    cout << "-------TEST B----" << endl;
    testB.show();
    cout << "外部通过内存地址访问类的private变量:" << endl << x1 << "-" << y1 << "-" << z1 <<"-"<<k1 << endl;
    return 0;
}

运行结果:
运行结果.png


点墨
26 声望3 粉丝

全栈前端开发工程师