C++学习笔记(四)--指针(3).doc_第1页
C++学习笔记(四)--指针(3).doc_第2页
C++学习笔记(四)--指针(3).doc_第3页
C++学习笔记(四)--指针(3).doc_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

C+学习笔记(四)-指针(3) 分类: C+学习笔记 2010-04-03 14:12 253人阅读 评论(0) 收藏 举报 C+指向类成员的指针的使用(详细介绍)1.首先普通函数指针不能被赋值为成员函数的地址,即使返回类型和参数完全匹配。例如:下面是的pfi是一个普通函数指针,它没有参数,返回类型为int:int (*pfi)();若有两个全局函数,HeightIs()和WidthIs():int HeightIs();int WidthIs();则下面的的赋值操作是合法的:pfi = HeightIs();pfi = WidthIs();但如今有一个类Screen也定义了两个访问函数height()和width(),它们也没有参数,返回类型也为int:inline int Screen:height() return _height; inline int Screan:width() return _width; 但是下面的赋值是非法的,会导致编译错误产生。pfi = &Screen:height();为什么会出现违例?因为,成员函数有一个非成员函数不具有的属性它的类(class)。指向成员函数的指针必须于其赋值的函数类型匹配,不是两个方面而是三个方面:(1)参数类型和个数(2)返回类型 (3) 它所属的类类型。成员函数指针和普通函数指针之间的不匹配是由于这两种指针在表示上的区别。函数指针存储函数的地址,可以被用来直接调用那个函数。成员函数指针首先必须被绑定在一个对象或者一个指针上,才能得到被调用对象的this 指针,然后才调用指针所指的成员函数。在下面将看到成员函数指针怎样被绑定到一个对象或指针上,以便调用一个成员函数。2.成员函数指针的声明:拿下面这个类来说明:class Screen public:/ 成员函数void home() _cursor = 0; void move( int, int );char get() return _screen_cursor; char get( int, int );bool checkRange( int, int );int height() return _height; int width() return _width; /.public:/修正string _screen;string:size_type _cursor;short _height;short _width;成员函数指针的声明要求扩展的语法,它要考虑类的类型。对指向类数据成员的指针也是这样。考虑Screen 类的成员_height 的类型。它的完整类型是”short 型的Screen 类的成员“指向_height 的指针的完整类型是”指向short 型的Screen 类的成员的指针“这可以写为:short Screen:*指向short型的Screen类的成员的指针定义如下:short Screen:* ps_Screen;ps_Screen 可以用_height 的地址初始化如下short Screen:*ps_Screen = &Screen:_height;在数据成员指针和普通指针之间的不匹配也是由于这两种指针的表示上的区别。普通指针含有引用一个对象所需的全部信息。数据成员指针在被用来访问数据成员之前,必须先被绑定到一个对象或指针上。定义一个成员函数指针需要指定函数返回类型,参数表和类。例如指向Screen 成员函数并且能够引用成员函数height()和width()的指针类型如下:int (Screen:*) ();这种类型指定了一个指向类Screen的成员函数的指针,它没有参数,返回值类型为int。指向成员函数的指针可被声明,初始化及赋值如下/ 所有指向类成员的指针都可以用0 赋值int (Screen:*pmf1)() = 0;int (Screen:*pmf2)() = &Screen:height;pmf1 = pmf2;pmf2 = &Screen:width;也可以用typedef 定义,这样语法更容易读。如:typedef Screen& (Screen:*Action)();Action default = &Screen:home;Action next = &Screen:forward;3.怎样使用指向类成员的指针 类成员的指针必须总是通过特定的对象或指向改类型的对象的指针来访问。是通过使用两个指向成员操作符的指针(针对类对象和引用的.* ,以及针对指向类对象的指针的-*)(操作符.*和-*的说明如下:pm-expression :cast-expressionpm-expression .* cast-expressionpm-expression -* cast-expressionThe binary operator .* combines its first operand, which must be an object of class type,with its second operand, which must be a pointer-to-member type. The binary operator -* combines its first operand, which must be a pointer to an object of class type, with its second operand, which must be a pointer-to-member type.In an expression containing the .* operator, the first operand must be of the class typeof the pointer to member specified in the second operand or of a type unambiguously derived from that class.In an expression containing the -* operator, the first operand must be of the type pointerto the class type of the type specified in the second operand, or it must be of a type unambiguously derived from that class.)如下面例子:int (Screen:*pmfi)() = &Screen:height;Screen& (Screen:*pmfS)( const Screen& ) = &Screen:copy;Screen myScreen, *bufScreen;/ 直接调用成员函数if ( myScreen.height() = bufScreen-height() )bufScreen-copy( myScreen );/ 通过成员指针的等价调用if ( (myScreen.*pmfi)() = (bufScreen-*pmfi)() )(bufScreen-*pmfS)( myScreen );类似地指向数据成员的指针可以按下列方式被访问:typedef short Screen:*ps_Screen;Screen myScreen, *tmpScreen = new Screen( 10, 10 );ps_Screen pH = &Screen:_height;ps_Screen pW = &Screen:_width;tmpScreen-*pH = myScreen.*pH;tmpScreen-*pW = myScreen.*pW;4.静态类成员的指针: 在非静态类成员的指针和静态类成员的指引之间有一个区别。指向类成员的指针语法不能被用来引用类的静态成员静态类成员。是属于该类的全局对象和函数它

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论