我們知道在C++中,為了防止某些數(shù)據(jù)成員或成員函數(shù)從外部被直接訪問,可以將它們聲明為private,這樣編譯器會阻止任何來自外部非友元的直接訪問。
那么我們真的就沒辦法直接修改類的私有數(shù)據(jù)成員了嗎?
來看一段代碼:
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
運(yùn)行后輸出結(jié)果為:
before pointer access:
test.a = 0
test.b = 0
after pointer access:
test.a = 1
test.b = 2.5
可以看到,雖然我們已經(jīng)將 A::a 和 A::b聲明為 private ,但在強(qiáng)大的指針面前,類的任何細(xì)節(jié)都暴露無遺。
某種程度上,類的一個(gè)對象可以看作包含不同類型元素的數(shù)組,其數(shù)據(jù)成員的地址偏移由數(shù)據(jù)成員在類定義中的順序決定——類對象的地址指向類中第一個(gè)被定義的數(shù)據(jù)成員的地址;第二個(gè)被定義的數(shù)據(jù)成員的地址取決于第一個(gè)數(shù)據(jù)成員的類型,若第一個(gè)為 int 型,則再偏移 4 個(gè)字節(jié)( sizeof(int) )即得到第二個(gè)數(shù)據(jù)成員的地址(有時(shí)也不一定是這樣,如上例中,由于類型對齊的緣故,實(shí)際偏移 8 個(gè)字節(jié)( sizeof(double) )才得到第二個(gè)數(shù)據(jù)成員的地址,具體內(nèi)容不在本文討論范圍內(nèi))。
所以即使你將其他所有細(xì)節(jié)都隱藏在 cpp 中,但頭文件中不可避免地會出現(xiàn)私有數(shù)據(jù)成員的聲明。而類的其他用戶只要能看到這個(gè)聲明,就能推算出類中各數(shù)據(jù)成員的相對地址偏移,從而用指針輕松實(shí)現(xiàn)對私有成員的訪問。
上例只對私有數(shù)據(jù)成員進(jìn)行了驗(yàn)證,有興趣的可以試試看用函數(shù)指針能否訪問私有成員函數(shù)。