1.对象指针的分类

一. 指向​​对象​​​的指针
二. 指向​​​对象成员​​的指针

对于​​对象成员​​我们可以划分为如下分类:

1.对象​​数据​​成员

2.对象成员​​函数​


2.对象指针的应用与要求

例题:用对象指针的方法输出时.分.秒。

#include<iostream>
using namespace std;
class Time
{
public:
Time(int, int, int);//声明结构函数
int hour;
int minute;
int sec;
void get_time();//声明公有成员函数

};

Time::Time(int h, int m, int s)//定义结构函数
{
hour = h;
minute = m;
sec = s;
}
void Time::get_time()//定义公有成员函数
{
cout << hour << ":" << minute << ":" << sec << endl;
}

int main()
{
Time t1(10, 13, 56);//定义Time类的对象t1并初始化
Time *pt;
pt = &t1;
cout << pt->hour <<":"<< pt->minute <<":"<< pt->sec << endl;
/*===================================================*/
int *p1;//定义指针变量p1
p1 = &t1.hour;//使p1指向t1.hour
cout << *p1 << endl;
/*====================================================*/
t1.get_time();//调用对象t1的公用成员函数get_time
Time *p2;
p2 = &t1;
p2->get_time();//调用p2所指对象t1的get_time函数
/*====================================================*/
void (Time::*p3)();
p3 = &Time::get_time;
(t1.*p3)();//调用对象t1中p3所指的成员函数(即t1.get_time())
/*====================================================*/
return 0;
}

Time *pt;//对象指针
Time t1;
pt=&t1;
cout<<pt->hour<<":"<<pt->minute<<":"<<pt->sec<<endl;

Time t1(10,13,56);
int *p1=&t1.hour;
cout<<*p1<<endl;

Time *p2;
p2 = &t1;
p2->get_time();//调用p2所指对象t1的get_time函数

void (Time::*p3)();
p3 = &Time::get_time;
(t1.*p3)();//调用对象t1中p3所指的成员函数(即t1.get_time())