Note: The following conclusions do not apply to member function pointers of classes. The member function pointers of classes are discussed separately.
1. pointer variables of any type can be directly assigned to const void *
Any type of non-const pointer variable can be directly assigned to void *
The const pointer variable cannot be directly assigned to void * unless it is forcibly converted
Class A {}; typedef int (* pfun) (string); // function pointer int * pint; const int * pint_c; char * pchar; const char * pchar_c; double * pdouble; const double * pdouble_c; A * pA; // user-defined type pointer const A * pa_c; pfun PF; // function pointer void * pvoid; const void * pvoid_c; // 1. pointer variables of any type can be directly assigned to const void * pvoid_c = pint; // okpvoid_c = pint_c; // Signature = pchar; // okpvoid_c = pchar_c; // okpvoid_c = pdouble; // okpvoid_c = pdouble_c; // okpvoid_c = PA; // okpvoid_c = pa_c; // okpvoid_c = PF; // OK // 2. void * pvoid = pint; // okpvoid = pchar; // okpvoid = pdouble; // okpvoid = PA; // okpvoid = PF; // OK // 3. the const pointer variable cannot be directly assigned to void * Unless pvoid = pint_c is forcibly converted by type conversion; // error: cannot convert from 'const int * 'to 'void *' pvoid = pchar_c; // error: cannot convert from 'const char * 'to 'void *' pvoid = pdouble_c; // error: cannot convert from 'const double * 'to 'void *' pvoid = pa_c; // error: cannot convert from 'const A * 'to 'void *' pvoid = (void *) pint_c; // okpvoid = (void *) pchar_c; // okpvoid = (void *) pdouble_c; // okpvoid = (void *) pa_c; // OK
2. Any type of pointer variables can be forced type conversion, including forced type conversion between const and non-const pointer variables.
Pint = (int *) pdouble; // okpint = (int *) PF; // okpint = (int *) pint_c; // OK: convert from const pointer variable to non-const pointer variable pint = (int *) pa_c; // OK: Convert from const pointer variable to non-const pointer variable Pa = (A *) pa_c; // OK: Convert the const pointer variable to the non-const pointer variable Pa = (A *) pdouble; // okpa = (A *) PF; // OK pF = (pfun) pdouble; // okpf = (pfun) Pa; // OK
Iii. Inheritance relationships between custom types: Child type pointer variables can be directly assigned to parent type pointer Variables
Parent type pointer variables cannot be directly assigned to child type pointer variables unless forced type conversion
Class A {}; Class B: public a // B inherited from a {}; Class C {}; A * pA; B * pb; C * PC; Pa = Pb; // OK: The child type pointer variable can be directly assigned to the parent type pointer variable Pb = PA; // error: the parent type pointer variable cannot be directly assigned to the child type pointer variable, unless forced type conversion // The following rules apply: Pa = (A *) PC; // okpb = (B *) Pa; // okpb = (B *) PC; // okpc = (C *) Pa; // okpc = (C *) Pb; // OK
Supplement:
1. the above principles are not applicable to member function pointers of classes.
class A{};typedef void (A::*AFunPointer)(void);typedef void (*FunPtr)(void);void * pVoid;int * pInt;FunPtr fp;AFunPointer afp;pVoid = afp; //error: cannot convert from 'AFunPointer' to 'void *'pInt = (int*)afp; //error: 'type cast' : cannot convert from 'AFunPointer' to 'int *'fp = (FunPtr)afp; //error: 'type cast' : cannot convert from 'AFunPointer' to 'FunPtr'afp = (AFunPointer)pInt; //error: 'type cast' : cannot convert from 'int *' to 'AFunPointer'afp = (AFunPointer)pVoid; //error: 'type cast' : cannot convert from 'void *' to 'AFunPointer'afp = (AFunPointer)fp; //error: 'type cast' : cannot convert from 'FunPtr' to 'AFunPointer'
It can be understood as follows: the member function pointer of a class is limited to the scope of a specific class, and cannot be converted to a pointer outside the domain.
2. Except for the member function pointer of the class, although all pointer variables of any type can be forced type conversion, that is, the const pointer can be strongly converted to a non-const pointer.
However, it should be noted that if the const pointer is strongly converted to a non-const pointer and used for the object of the original const, undefined behavior will be generated (this is not specified in C ++ ). For example:
Const int A = 50; // define the const variable and constant const int * P = & A; // The const pointer Variable P points to the const variable aint * q = (int *) P; // If the const pointer is strongly converted to a non-const pointer, the non-const pointer variable q points to the const variable A * q = 56; // The behavior is undefined, the following output is the output result cout in vs2008 <A <Endl; // output: 50 cout <* P <Endl; // output: 56. Obviously, P does not point to a. cout <* q <Endl; // output: 56. Obviously, Q does not point to a anymore.
3. For the forced conversion of general function pointers, the following are of course OK.
class A;typedef void (*pFun1)(int, int);typedef int (*pFun2)(A*, double);pFun1 pf1;pFun2 pf2;pf2 = (pFun2)pf1; // OK