C++成员解除引用运算符的示例详解

Wanda ·
更新时间:2024-09-20
· 1973 次阅读

下面看下成员解除引用运算符,C++允许定义指向类成员的指针,对这种指针进行声明或解除引用时,需要使用一种特殊的表示法。
例:

class Example { private: int feet; int inches; public: Example(); Example(int ft); ~Example(); void show_in()const; void show_ft()const; void use_ptr()const; };

如果没有具体的对象,则inches成员只是一个标签。也就是说,这个类将inches定义为一个成员标识符,但要为它分配内存。必须声明这个类的一个对象:

Example ob;//现在ob.inches存在

因此,可以结合使用标识符inches和特定的对象,来引用实际的内存单元(对于成员函数,可以省略对象名,但对象被认为是指针执行对象)。
C++允许这样定义一个指向标识符inches的成员指针:

int Example::*pt = &Example::inchers;

这种指针与常规指针有所区别。常规指针指向特定的单元格,而pt指针并不是指向特定的内存单元,因为声明中没有指出具体的对象。指针pt指的是inches成员在任意Example对象中的位置。和标识符inches一样,pt被设计与对象标识符要求使用。实际上。表达式*pt对标识符inches的角色做了假设,因此,可以使用对象标识符来指定访问的对象,使用pt指针来指定该对象的inches成员。例如:类方法可以使用下面得的代码:

int Example::*pt = &Example::inches; Example ob1; Example ob2; Example *pq = new Example; cout<<ob1.*pt<<endl;//ob1对象的inches成员 cout<<ob2.*pt<<endl;//ob2对象的inches成员 cout<<po->*pt<<endl;//*po对象的inches成员

其中,*和->*都是成员解除运算符,声明对象后,ob1.*pi指的将是ob1对象的inches成员,同样,pq->*pt指的是pq指向的对象的inxhes成员。
改变上述示例中使用的对象,将改变使用的inches成员。不过也可以修改pt指针本身。由于feet的类型与inches相同,因此可以将pt重新设置为指向feet成员(而不指向inches成员),这样ob1.*pt将是ob1的feet成员:

pt = &Example::feet; cout<<ob1.*pt<<endl;//*pt相当于成员名,可用标识(相同类型)其他成员

可以使用成员指针标识成员函数,其语法稍微复杂的。对于不带任何参数、返回值为void的函数,声明一个指向函数的指针:

void (*pf)();//pf 指向函数

声明指向成员函数指针时,必须指出该函数所属的类。例:

void (Example::*pf)()const;//pf指向类成员函数

表明pf可用于使用Example方法地方。且Example::*pf必须放在括号中,可以将特定成员函数的地址赋给指针:

pf = &Example::show_inches;

注意,与普通函数指针的赋值情况不同,这里必须使用地址运算符,完成赋值操作后,便可以使用一个对象来调用该成员函数:

Example ob3(20); (ob3.*pf)();//使用ob3对象调用show_feet()

必须将ob3*p放在括号中,以明确地指出,该表达式表示的是一个函数名。
由于show_feet()原型与show_inches()相同,因此也可以使用pf来访问show_feet()方法:

pf = &Example::show_feet; (ob3*pf)();//将show_feet()应用于ob3对象

例:
下面程序use_ptr()方法,使用成员指针来访问Example类的数据成员和函数成员。

#include <iostream> using namespace std; class Example { private: int feet; int inches; public: Example(); Example(int ft); ~Example(); void show_in()const; void show_ft()const; void use_ptr()const; }; Example::Example() { feet=0; inches=0; } Example::Example(int ft) { feet=ft; inches=12*feet; } Example::~Example() { } void Example::show_in()const { cout<<inches<<"inches\n"; } void Example::show_ft()const { cout<<feet<<"feet\n"; } void Example::use_ptr()const { Example yard(3); int Example::*pt; pt=&Example::inches; cout<<"Set pt to &Example::inches:\n"; cout<<"this->pt:"<<this->*pt<<endl; cout<<"yard.*pt:"<<yard.*pt<<endl; pt=&Example::feet; cout<<"Set pt to &Example::inches:\n"; cout<<"this->pt:"<<this->*pt<<endl; cout<<"yard.*pt:"<<yard.*pt<<endl; void (Example::*pf)()const; pf=&Example::show_in; cout<<"Set pf to &Example::show_in:\n"; cout<<"Using (this->*pf)():"; (this->*pf)(); cout<<"Using (yard.*pf)():"; (yard.*pf); } int main() { Example car(15); Example van(20); Example garage; cout<<"car.usr_ptr() output:\n"; car.use_ptr(); cout<<"\nvan.use_ptr()outptr:\n"; van.use_ptr(); return 0; }

本例子在编译期间给指针赋值,在更为复杂的类中,可以使用指向数据成员和方法的成员指针。以便在运行阶段确定与指针关联的成员。

到此这篇关于C++成员解除引用运算符的示例详解的文章就介绍到这了,更多相关C++解除引用运算符内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!



c+ 运算符 示例 C++

需要 登录 后方可回复, 如果你还没有账号请 注册新账号