Pointers to Class Members (.* and ->*)

C++ provides the .* and ->* operators for accessing class members via pointers. This is a rarely used capability that is used primarily by advanced C++ programmers. We provide only a mechanical example of using pointers to class members here. Figure 24.6 demonstrates the pointer-to-class-member operators.

Figure 24.6. Demonstrating the .* and ->* operators.

(This item is displayed on pages 1211 - 1212 in the print version)

1 // Fig. 24.6: fig24_06.cpp 2 // Demonstrating operators .* and ->*. 3 #include 4 using std::cout; 5 using std::endl; 6 7 // class Test definition 8 class Test 9 { 10 public: 11 void test() 12 { 13 cout << "In test function "; 14 } // end function test 15 16 int value; // public data member 17 }; // end class Test 18 19 void arrowStar( Test * ); // prototype 20 void dotStar( Test * ); // prototype 21 22 int main() 23 { 24 Test test; 25 test.value = 8; // assign value 8 26 arrowStar( &test ); // pass address to arrowStar 27 dotStar( &test ); // pass address to dotStar 28 return 0; 29 } // end main 30 31 // access member function of Test object using ->* 32 void arrowStar( Test *testPtr ) 33 { 34 void ( Test::*memPtr )() = &Test::test; // declare function pointer 35 ( testPtr->*memPtr )(); // invoke function indirectly 36 } // end arrowStar 37 38 // access members of Test object data member using .* 39 void dotStar( Test *testPtr2 ) 40 { 41 int Test::*vPtr = &Test::value; // declare pointer 42 cout << ( *testPtr2 ).*vPtr << endl; // access value 43 } // end dotStar  

In test function 8  


The program declares class Test (lines 817), which provides public member function test and public data member value. Lines 1920 provide prototypes for the functions arrowStar (defined at lines 3236) and dotStar (defined at lines 3943), which demonstrate the ->* and .* operators, respectively. Lines 24 creates object test, and line 25 assigns 8 to its data member value. Lines 2627 call functions arrowStar and dotStar with the address of the object test.

Line 34 in function arrowStar declares and initializes variable memPtr as a pointer to a member function. In this declaration, Test::* indicates that the variable memPtr is a pointer to a member of class Test. To declare a pointer to a function, enclose the pointer name preceded by * in parentheses, as in (Test::*memPtr). A pointer to a function must specify, as part of its type, both the return type of the function it points to and the parameter list of that function. The return type of the function appears to the left of the left parenthesis and the parameter list appears in a separate set of parentheses to the right of the pointer declaration. In this case, the function has a void return type and no parameters. The pointer memPtr is initialized with the address of class Test's member function named test. Note that the header of the function must match the function pointer's declarationi.e., function test must have a void return type and no parameters. Notice that the right side of the assignment uses the address operator (&) to get the address of the member function test. Also, notice that neither the left side nor the right side of the assignment in line 34 refers to a specific object of class Test. Only the class name is used with the binary scope resolution operator (::). Line 35 invokes the member function stored in memPtr (i.e., test), using the ->* operator. Because memPtr is a pointer to a member of a class, the ->* operator must be used rather than the -> operator to invoke the function.

Line 41 declares and initializes vPtr as a pointer to an int data member of class Test. The right side of the assignment specifies the address of the data member value. Line 42 dereferences the pointer testPtr2, then uses the .* operator to access the member to which vPtr points. Note that the client code can create pointers to class members for only those class members that are accessible to the client code. In this example, both member function test and data member value are publicly accessible.


Common Programming Error 24.2

Declaring a member-function pointer without enclosing the pointer name in parentheses is a syntax error.

Common Programming Error 24.3

Declaring a member-function pointer without preceding the pointer name with a class name followed by the scope resolution operator (::) is a syntax error.

Common Programming Error 24.4

Attempting to use the -> or * operator with a pointer to a class member generates syntax errors.

Категории