這里給出一個方法,實現對某個方法調用者的限制。
1 class TestBase{
2 int data;
3 protected:
4 void write(){};
5 public:
6 void read(){};
7 };
8
9 class Test:public TestBase{
10 friend class client;
11 };
12
13 Test test;
14 TestBase test2;
15
16 class client{
17 public:
18 void dowith(){
19 test.write();
20 //test2.write();
21 };
22 };
23 //-----------
24 int main()
25 {
26 client c;
27 c.dowith();
28 return 0;
29 }
這里將寫方法聲明為保護的,對外界不可訪問。派生類Test的作用僅僅是將友元類進行聲明。
=======================
妙妙妙!
使用這個方法,就可以對類的私有函數進行單元測試了!!!!
JeansBer