類的對象指針和二級指針
1 #include <iostream>
2
3 class myclass
4 {
5 public:
6 int x;
7 int y;
8 public:
9 myclass(int, int);//構造函數
10 void printxy();
11 };
12
13 myclass::myclass(int a, int b) :x(a), y(b)//構造函數
14 {
15
16 }
17
18 void myclass::printxy()
19 {
20 std::cout << x << " " << y << std::endl;
21 }
22
23 void main()
24 {
25 myclass *p(new myclass(10, 9));//一級指針
26
27 p->printxy();
28
29 myclass class1(20, 1);
30
31 myclass **pp = &p;//二級指針
32
33 (*pp)->printxy();//一級指針
34 (**pp).printxy();//0級指針
35
36 system("pause");
37 }
1 #include <iostream>
2
3 class myclass
4 {
5 public:
6 int x;
7 int y;
8 public:
9 myclass(int, int);//構造函數
10 void printxy();
11 };
12
13 myclass::myclass(int a, int b) :x(a), y(b)//構造函數
14 {
15
16 }
17
18 void myclass::printxy()
19 {
20 std::cout << x << " " << y << std::endl;
21 }
22
23 myclass class1(10, 11);
24 myclass class2(11, 12);
25
26 void change1(myclass **pp)//二級指針
27 {
28 *pp = &class2;
29 }
30
31 void change2(myclass * & p)//引用一個指針
32 {
33 p = &class1;
34 }
35
36 void main()
37 {
38 myclass *p = &class1;
39
40 p->printxy();
41
42 change1(&p);
43
44 p->printxy();
45
46 change2(p);
47
48 p->printxy();
49
50 system("pause");
51 }