指向类成员的指针 (pointers to class members)

很少看到指针的这种用法,下面借助MSDN开始介绍介绍.

Pointers to Class Members

These pointers define a type that points to a class member of a particular type. Such a pointer can be used by any object of the class type or any object of a type derived from the class type.

Use of pointers to class members enhances the type safety of the C++ language. Three new operators and constructs are used with pointers to members, as shown in Table 2.5.

指向类成员的指针定义了一个类型,这种类型指向了特殊类的类成员(包括数据成员和函数成员).这种类型的指针可被这个类以及它的派生类的任何对象所使用.指向类成员指针的使用增强了C++语言的类型安全性.随着指向类成员指针类型的出现三种新的操作符与构造.如表格5所示.

Table 2.5  Operators and Constructs Used with Pointers to Members

Operator or
Construct


Syntax


Use

::*(构造)

type::*ptr-name

Declaration of pointer to member. The type specifies the class name, and ptr-name specifies the name of the pointer to member. Pointers to members can be initialized. For example:

 

 

MyType::*pMyType = &MyType::i;
         
         

.* (操作符)

obj-name.*ptr-name

Dereference a pointer to a member using an object or object reference. For example:

 

 

int j = Object.*pMyType;
         
         

–>* (操作符)

obj-ptr–>*ptr-name

Dereference a pointer to a member using a pointer to an object. For example:

 

 

int j = pObject->*pMyType;
         
         

 

Consider this example that defines a class AClass and the derived type pDAT, which points to the member I1:

#include <iostream.h>
      
      

   
   
    
     
   
   
// Define class AClass.
      
      
class AClass
      
      
{
      
      
public:
      
      
    int I1;
      
      
    Show() { cout << I1 << "/n"; }
      
      
};
      
      

   
   
    
     
   
   
// Define a derived type pDAT that points to I1 members of
      
      
// objects of type AClass.
      
      
int AClass::*pDAT = &AClass::I1;
      
      

   
   
    
     
   
   
void main()
      
      
{
      
      
    AClass aClass;             // Define an object of type AClass.
      
      
    AClass *paClass = &aClass;  // Define a pointer to that object.
      
      

   
   
    
     
   
   
    int i;
      
      

   
   
    
     
   
   
    aClass.*pDAT = 7777;      // Assign to aClass::I1 using .* operator.
      
      
    aClass.Show();
      
      

   
   
    
     
   
   
    i = paClass->*pDAT;       // Dereference a pointer 
      
      
                              //  using ->* operator.
      
      
    cout << i << "/n";
      
      
}
output: 7777
        7777

The pointer to member pDAT is a new type derived from class AClass. It is more strongly typed than a “plain” pointer to int because it points only to int members of class AClass (in this case, I1). Pointers to static members are plain pointers rather than pointers to class members. Consider the following example:

指向类数据成员的指针pDAT是 派生自类AClass的新类型.  相对平常指向整型的指针,强烈建议用它. 因为它只指向类AClass的int的数据成员.   指向类中的静态数据成员的指针应该用平常的指针而不是用指向类成员的指针.(我想是因为静态数据成员属于类,不能用  .* ,->*的操作符.)

class HasStaticMember
      
      
{
      
      
public:
      
      
    static int SMember;
      
      
};
      
      
int HasStaticMember::SMember = 0;
      
      

   
   
    
     
   
   
int *pSMember = &HasStaticMember::SMember;
      
      

Note that the type of the pointer is “pointer to int,” not “pointer to HasStaticMember::int.”

Pointers to members can refer to member functions as well as member data. Consider the following code:

注意: 指针的类型是指向整型的指针,而不是指向 HasStaticMember::int的指针. 指向类成员的指针同样也可以用于指向类函数成员指针.(用法与指向类数据成员的指针的用法类似).

#include <stdio.h>
      
      

   
   
    
     
   
   
// Declare a base class, A, with a virtual function, Identify.
      
      
// (Note that in this context, struct is the same as class.)
      
      
struct A
      
      
{
      
      
    virtual void Identify() = 0; // No definition for class A.
      
      
};
      
      

   
   
    
     
   
   
// Declare a pointer to the Identify member function.
      
      
void (A::*pIdentify)() = &A::Identify;
      
      

   
   
    
     
   
   
// Declare class B derived from class A.
      
      
struct B : public A
      
      
{
      
      
    void Identify();
      
      
};
      
      

   
   
    
     
   
   
// Define Identify functions for classe B
      
      
void B::Identify()
      
      
{
      
      
    printf( "Identification is B::Identify/n" );
      
      
}
      
      

   
   
    
     
   
   
void main()
      
      
{
      
      
    B  BObject;          // Declare objects of type B
      
      
    A *pA;               // Declare pointer to type A.
      
      

   
   
    
     
   
   
    pA = &BObject;       // Make pA point to an object of type B.
      
      
    (pA->*pIdentify)();  // Call Identify function through pointer
      
      
                         //  to member pIdentify.
      
      
}
      
      

The output from this program is:

Identification is B::Identify
      
      

The function is called through a pointer to type A. However, because the function is a virtual function, the correct function for the object to which pA refers is called.

 通用指向类型A的指针来调用函数.因为函数是虚函数,(根据多态性),所以应该调用B的.

 

有些根据自己的意思,没有按照MSDN来翻译.由于英语水平有限,翻译错之处,请见谅.

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值