有关对象指针的使用方法

 1 #include <iostream>
 2 
 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 4 using namespace std;
 5 class Time
 6 {
 7     public:
 8         Time(int,int,int);
 9         int hour;
10         int minute;
11         int sec;
12         void get_time();
13 };
14 
15 Time::Time(int h,int m,int s)
16 {
17     hour=h;
18     minute=m;
19     sec=s;
20 }
21 
22 void Time::get_time()
23 {
24     cout<<hour<<":"<<minute<<":"<<sec<<endl;
25 }
26 
27 int main(int argc, char** argv) {
28     Time t1(10,13,56);
29     int *p1=&t1.hour;
30     cout<<*p1<<endl;
31     t1.get_time();
32     Time *p2=&t1;
33     p2->get_time();
34     void(Time::*p3)();
35     p3=&Time::get_time;
36     (t1.*p3)();
37     return 0;
38 }

 

posted @ 2018-08-01 16:26  borter  阅读(127)  评论(0编辑  收藏  举报