我们知道在C++中,为了防止某些数据成员或成员函数从外部被直接访问,可以将它们声明为private,这样编译器会阻止任何来自外部非友元的直接访问。
那么我们真的就没办法直接修改类的私有数据成员了吗?
来看一段代码:
 1 #include <iostream>
 2 using namespace std;
 3 
 4 
 5 class A
 6 {
 7 private:
 8     // a & b are private members of class A
 9     int a;
10     double b;
11 public:
12     // initialize a & b as value 0
13     A() : a(0), b(0) {}
14 
15 public:
16     // for display the value of a & b
17     int GetA();
18     double GetB();
19 };
20 
21 int A::GetA()
22 {
23     return a;
24 }
25 
26 double A::GetB()
27 {
28     return b;
29 }
30 
31 int _tmain(int argc, _TCHAR* argv[])
32 {
33     A test;
34 
35     cout << "before pointer access:\n"
36          << " test.a = " << test.GetA() << "\n"
37          << " test.b = " << test.GetB() << "\n" << endl;
38 
39     // access object test of class A by pointer
40     int* privateA = reinterpret_cast<int*>(&Test);
41     double* privateB = reinterpret_cast<double*>(&Test) + 1;
42 
43     // value changing by pointer!
44     *privateA = 1;
45     *privateB = 2.5;
46 
47     cout << "after pointer access:\n"
48          << " test.a = " << test.GetA() << "\n"
49          << " test.b = " << test.GetB() << "\n" << endl;
50 
51     return 0;
52 }
53 
运行后输出结果为:
before pointer access:
 test.a = 0
 test.b = 0
after pointer access:
 test.a = 1
 test.b = 2.5
可以看到,虽然我们已经将 A::a 和 A::b声明为 private ,但在强大的指针面前,类的任何细节都暴露无遗。
某种程度上,类的一个对象可以看作包含不同类型元素的数组,其数据成员的地址偏移由数据成员在类定义中的顺序决定——类对象的地址指向类中第一个被定义的数据成员的地址;第二个被定义的数据成员的地址取决于第一个数据成员的类型,若第一个为 int 型,则再偏移 4 个字节( sizeof(int) )即得到第二个数据成员的地址(有时也不一定是这样,如上例中,由于类型对齐的缘故,实际偏移 8 个字节( sizeof(double) )才得到第二个数据成员的地址,具体内容不在本文讨论范围内)。
所以即使你将其他所有细节都隐藏在 cpp 中,但头文件中不可避免地会出现私有数据成员的声明。而类的其他用户只要能看到这个声明,就能推算出类中各数据成员的相对地址偏移,从而用指针轻松实现对私有成员的访问。
上例只对私有数据成员进行了验证,有兴趣的可以试试看用函数指针能否访问私有成员函数。