//指向对象的指针
#include<iostream>
using namespace std;
class Box
{
public:
Box();
Box(int h, int w,int len):
height(h),width(w),length(len){};
int v();
private:
int height;
int width;
int length;
};
Box::Box()
{
height=10;
width=10;
length=10;
}
int Box::v()
{
return(height*width*length);
}
int main()
{
Box *p1,*p2; //定义指向类型为Box的指针
Box box1;
p1=&box1; //把box1的地址赋给p1
cout<<(*p1).v()<<endl;
Box box2(12,23,10);
p2=&box2; //把box2的地址赋给p2
cout<<p2->v()<<endl;
return 0;
}
//指针指向成员函数
#include<iostream>
using namespace std;
class Time
{
public:
Time(int h,int m,int s)
{
hour=h;
minute=m;
sec=s;
}
int hour;
int minute;
int sec;
void get_time();
};
void Time::get_time()
{
cout<<"时间是:"<<hour<<":"<<minute<<":"<<sec<<endl;
}
int main()
{
Time t1(23,12,34);
int *p1=&t1.hour; //指针指向数据成员
cout<<"已经"<<*p1<<"点"<<endl;
Time *p2;
p2=&t1; //指针指向对象
(*p2).get_time();
void (Time::*p3)(); //指针指向成员函数
p3=&Time::get_time;
(t1.*p3)();
(p2->*p3)();
return 0;
}