1. Declaring member pointers
Sometimes we want to get a pointer to a member directly and then get that member from an object or another object, and then we need to use the member pointer. A member pointer that contains the type of the class and the type of the member.
The member pointer is only for non-static members of the class. The static class member is not part of any object, so no special syntax is required to point to the static member, and the static member pointer is a normal pointer.
Note: it points to a specific member of a class, not to a specific member of a particular object.
Member Pointer definition format: member type class name::* pointer name =& class name:: member name, member function pointer definition Format: member function return type class name::* pointer name = & Class Name:: member function name (parameter table); A pointer to a member function must match the type that it points to in three respects: 1) The type and number of the function parameter, including whether the member is a const;2) return type, 3) the type of the owning class. Defines a pointer to a member function by specifying the function return type, formal parameter list, and class.
The difference between a normal pointer and a member pointer:
1) The normal pointer is initialized with the address of the determined object, pointing to a definite object;
2) The member pointer is initialized with the member of the class (note that it is not a member of the object) (that is, only the offset information, not the initial address).
2. Non-retrograde pointer to member
Transferred from: http://developer.zdnet.com.cn/developer/code/story/0,3800066897,39308240,00.htm
Pointers to a member of a base class can be converted to pointers to the corresponding members of the derived class. However, in turn, it does not work. This rule is called "the irreversibility of pointers to members". I will explain this irreversible feature and the underlying rationale behind it.
Consider the following two classes:
struct Base { virtual void func ();}; struct Derived:base { void func ();};
A pointer to a member of Class Base cannot refer to the corresponding member of the class Derived:
typedef void (Base::* PMF) (); PMF pmf1=&derived::func; ERRORPMF pmf2=&base::func; finebase* ptr=new Derived; Fine
However, if you define a pointer to a member of the class Derived, you can implicitly turn it into a pointer to the corresponding Member of Class Base:
typedef void (Derived::* PMF) (); PMF pmf1=&derived::func; Fine PMF pmf2=&base::func; Also fine
Fundamentals
This irreversible rule seems counterintuitive. When working with generic pointers and references, you can implicitly turn a pointer to a derived class into a pointer to its base class, but not to the contrary:
Base * p = new Derived; OK Derived *p = new Base; Error, can ' t convert ' Derived * ' to ' Base * '
However, in terms of pointers to members, the opposite is true. This is necessary because a derived class has all the members of the base class it inherits from, so any pointer to a member of the base class can be mapped to the corresponding member of the derived class. The opposite does not work because derived classes may have more members that do not exist in the base class.
Citations:
Baidu Encyclopedia: http://baike.baidu.com/link?url=- Yrqz4cumrcocogvc3c6bqcrtszzkfzdji7t3gji1tvdvjqfocw4hrnblwenriawzms4dltuq4xbx79f8uacya
[C + + primer] class member pointers