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

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


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


In test function
8


Fig. 21.6. Demonstrating operators .* and ->*.

The program declares class Test (lines 7–16), which provides public member function test and public data member value. Lines 18–19 provide prototypes for the functions arrowStar (defined in lines 30–34) and dotStar (defined in lines 37–41), which demonstrate the ->* and .* operators, respectively. Line 23 creates object test, and line 24 assigns 8 to its data member value. Lines 25–26 call functions arrowStar and dotStar with the address of the object test.

Line 32 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 function’s return type 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. The header of the function must match the function pointer’s declaration—i.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 32 refers to a specific object of class Test. Only the class name is used with the scope resolution operator (::). Line 33 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 39 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 40 dereferences the pointer testPtr2, then uses the .* operator to access the member to which vPtr points. 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.


Image Common Programming Error 21.2

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



Image Common Programming Error 21.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.



Image Common Programming Error 21.4

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


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.176.68