当前位置:嗨网首页>书籍在线阅读

07-成员解除引用运算符

  
选择背景色: 黄橙 洋红 淡粉 水蓝 草绿 白色 选择字体: 宋体 黑体 微软雅黑 楷体 选择字体大小: 恢复默认

E.2 成员解除引用运算符

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; // now ob.inches exists

因此,可以结合使用标识符inches和特定的对象,来引用实际的内存单元(对于成员函数,可以省略对象名,但对象被认为是指针指向的对象)。

C++允许这样定义一个指向标识符inches的成员指针:

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

这种指针与常规指针有所差别。常规指针指向特定的内存单元,而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; // display inches member of ob1
cout << ob2.*pt << endl; // display inches member of ob2
cout << po->*pt << endl; // display inches member of *po

其中,和->都是成员解除引用运算符(member dereferencing operator)。声明对象(如obl)后,ob1.pi指的将是ob1对象的inches成员。同样,pq->pt指的是pq指向的对象的inches成员。

改变上述示例中使用的对象,将改变使用的inches成员。不过也可以修改pt指针本身。由于feet的类型与inches相同,因此可以将pt重新设置为指向feet成员(而不指向inches成员),这样ob1.*pt将是ob1的feet成员:

pt = &Example::feet;     // reset pt
cout << ob1.*pt << endl; // display feet member of ob1

实际上,*pt相当于一个成员名,因此可用于标识(相同类型的)其他成员。

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

void (*pf)(); // pf points to a function

声明指向成员函数的指针时,必须指出该函数所属的类。例如,下面的代码声明了一个指向Example类方法的指针:

void (Example::*pf)() const; // pf points to an Example member function

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

pf = &Example::show_inches;

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

Example ob3(20);
(ob3.*pf)(); // invoke show_inches() using the ob3 object

必须将ob3.*pf放在括号中,以明确地指出,该表达式表示的是一个函数名。

由于show_feet()的原型与show_inches()相同,因此也可以使用pf来访问show_feet()方法:

pf = &Example::show_feet;
(ob3.*pf)();       // apply show_feet() to the ob3 object

程序清单E.1中的类定义包含一个use_ptr()方法,该方法使用成员指针来访问Example类的数据成员和函数成员。

程序清单E.1 memb_pt.cpp

// memb_pt.cpp -- dereferencing pointers to class members
#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::feet:\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.use_ptr() output:\n”;
    car.use_ptr();
    cout << “\nvan.use_ptr() output:\n”;
    van.use_ptr();
    return 0;
}

下面是程序清单E.1中程序的运行情况:

car.use_ptr() output:
Set pt to &Example::inches:
this->pt: 180
yard.*pt: 36
Set pt to &Example::feet:
this->pt: 15
yard.*pt: 3
Set pf to &Example::show_in:
Using (this->*pf)(): 180 inches
Using (yard.*pf)(): 36 inches
van.use_ptr() output:
Set pt to &Example::inches:
this->pt: 240
yard.*pt: 36
Set pt to &Example::feet:
this->pt: 20
yard.*pt: 3
Set pf to &Example::show_in:
Using (this->*pf)(): 240 inches
Using (yard.*pf)(): 36 inches

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