C++语言基础 例程 对象指针
2015-03-25 20:32
405 查看
贺老师的教学链接 本课讲解
示例:使用指向对象数据成员的指针
示例:使用指向对象成员函数的指针
示例:使用指向对象数据成员的指针
#include <iostream> using namespace std; class Time { public: Time(int,int,int); void get_time( ); private: int hour,minute,sec; }; 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 t(10,13,56); int *p1; p1 = &t.hour; //这个地方有错 cout<<*p1<<endl; return 0; }
示例:使用指向对象成员函数的指针
#include <iostream> using namespace std; class Time { public: Time(int,int,int); void get_time( ); private: int hour,minute,sec; }; 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 t(10,13,56); void (Time::*p)( ); p=&Time::get_time; //而非p2=&t1.get_time; (t.*p)( ); return 0; }
相关文章推荐
- C++语言基础 例程 this指针
- C++语言基础 例程 类和对象的简单应用举例
- C++语言基础 例程 C++的输入输出与流对象
- C++语言基础 例程 初见对象
- C++语言基础 例程 类的声明和对象的定义
- C++语言基础 例程 基于对象的程序的执行过程
- C++语言基础 例程 对象数组
- C++语言基础 例程 对象成员的引用
- C++语言基础 例程 对象的动态建立和释放
- C++语言基础 例程 用const实施保护
- C/C++开发语言系列之13---C++对象指针—指向对象的指针
- C++语言基础 例程 调用构造函数和析构函数的顺序
- C++语言基础 例程 应用系统开发:银行储蓄系统
- C++语言基础 例程 重载双目运算符
- C++语言基础 例程 类声明和成员函数定义的分离
- C++语言基础 例程 运算符重载的方法
- C++语言基础 例程 案例:Time类的设计
- C++语言基础 例程 Time类的设计
- C++语言基础 例程 析构函数
- C++语言基础 例程 默认构造函数