#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类对象并初始化 int *p1=&t1.hour;//定义指向对象数据成员的指针变量,即p1指向t1.hour; cout<<*p1<<endl;//*p1=t1.hour t1.get_time(); Time *p2=&t1;//定义指向对象的指针变量,即p2指向t1; p2->get_time();//相当于t1.get_time() void(Time:: *p3)();//定义指向公用成员函数的指针变量p3; p3=&Time::get_time; (t1.*p3)();//*p3=get_time,所以相当于t1.get_time() return 0; }
尝试有关对象指针的使用方法,里面有自己加的知识,更深入了解了
努力加载评论中...
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步