Pointers to class member functions and data fields of the C++ classes
Pointers to the member functions are rarely used, and in general, it might be better to avoid using them because there is a good chance that the reader of the program will fail to understand what this notation means. Nevertheless, sometimes they are helpful. From time to time they are used in the code, and it is necessary to understand or modify such code.
The idea of the pointers to members is similar to simple pointers. Simple pointer can point either to a piece of data or to a function. Member pointer can point either to a data field of a structure (union/class) or to a member function. These pointers are always typed. Data member pointer can point to any non-static member of the structure that has specific type. Function member pointer can point to any non-static member function that has certain signature. It makes sense to use member pointers only when structure or class has more than one data member of the same type or when several member functions share the same signature. C++ does not have a concept of a member references.
Main difference between simple pointers and member pointers is that using the member pointer requires an instance of the object. An instance of the object tells what object should be processed and the value of the pointer tells what data field of the object should be used or what member function should be called. Value of the member pointer is conceptually equivalent to the offset from the beginning of the object to its member.
Look at the small sample program:
This program has a class that has two member functions and two data fields. The function main below uses member pointers to access elements of this class.
Here are pieces of the C++ grammar that are related to assembling the declarator:
Rules R109 and R110 tell that declarator can contain any number of PtrOperator symbols. Note that these rules also tell that PtrOperator symbols should be considered only after assembling the DirectDeclarator. Rules R111 and R114 show how ParameterDeclarationClause is added. This symbol describes parameters of the function. Finally the rule R138 describes one of the possible forms of the PtrOperator. Other forms of the PtrOperator are not shows here.
Note that when the pointers to member functions are defined parentheses are used in a similar way when simple pointers to functions are defined. Actually these parentheses tell that this is a pointer to function. Without parentheses these declarations will describe a function that returns a pointer. Defining data member pointers does not require parentheses.
These pieces of the C++ grammar are used for parsing expressions that contain pointers to the class members:
Rules R421, R422 and R423 show that operators for using the member pointers are simple binary operators from the grammar stand point. Rules R470, R478 and R479 describe scanning the function call. Note that rules for function call operate with a lower level non-terminal than the rules that describe using the member pointers. This means that function call operation has higher priority. This is why lines 21 and 31 have parentheses. Similar thing happens when simple pointers to functions are used.
Once again, the member pointer is always typed. Using a simple pointer is an unary operation, while using a member pointer is a binary operation. The first operand is an instance of the object and the value of the member pointer describes to the member of the class that should be used.
Notes on the base classes and derived classes. C++ can silently cast derived class to its base class. This rule also appllies to the member pointers:
If you find this article useful or if you want to propose topic for a similar article, send us a note using the contacts page.