pointers, which have always been very favored in the C + + language; A C + + application that does not use pointers is almost impossible to find. The address used to store data and programs, which is the basic function of pointers. Used to point to integers, with integer pointers (int*), floating point pointers (float*) pointing to floating-point numbers, pointing to structures, using corresponding structure pointers (struct XXX *), pointing to arbitrary addresses, with no type pointer (void*).
Sometimes, we need some generic pointers. In C, (void*) can represent everything, but in C + +, we have some special pointers that cannot be represented by (void*). In fact, in C + +, trying to find a generic pointer, especially a generic function pointer, is simply an "impossible task."
C + + is a static type of language, and type safety is important in C + +. In C, you can use void* to point to everything, but in C + +, void* does not point to everything, and even if it does, it loses the meaning of type safety. Type safety can often help us find some potential bugs in the program.
Let's explore how to store pointers to various types of data in C + +.
1. Data pointers
Data pointers are divided into two types: general data pointers and member data pointers
1.1 General Data pointers
This does not have to be clear, as with C language, definition, assignment is very simple and clear. Common are: int*, double* and so on.
Such as:
int value = 123;
int * pn = &value;
1.2 Member data pointers
A structure that resembles the following:
struct MyStruct
{
int key;
int value;
};
Now there is a struct object:
MyStruct me;
mystruct* pMe = &me;
We need the address of the value member, we can:
int * Pvalue = &me.value;
Or
int * Pvalue = &pMe->value;
Of course, this pointer is still part of the first areas-general data pointer.
Well, now we need a pointer that points to any data member in the MyStruct, so it's supposed to be a child:
int mystruct::* PMV = &MyStruct::value;
Or
int mystruct::* PMK = &MyStruct::key;
The purpose of this pointer is to obtain the address of the struct member within the structure. We can use this pointer to access the member data:
int value = pme->*pmv;//Get PMe value member data.
int key = ME.*PMK//Get key member data for me.