Before beginning, the first science cout the knowledge of pointers, so as to be well understood in the test program:
Look at the following code:
void Main (void) {int a = 10;int *p = &a;cout << a << endl;cout << p << endl;cout << *p &L t;< endl;cout << &a << Endl;}
Execution Result:
From the code and execution results can be seen cout implementation principle:cout<< followed by a variable identifier, and then convert the identifier to the corresponding memory address of the house number, and then output the data stored in the number.
In particular, when:cout<< a pointer, to understand two things, the result of the output is the number of the memory unit pointed to by the pointer, but also to figure out what the pointer variable is actually stored in the memory of the memory unit. For example: cout<<p This output is the number of the memory unit that P points to, which is the house number of a, but the actual location of P is in the memory space of P.
Look at a complex model:
- &d indicates that the pointer of this class object,cout<< (&D) outputs the memory address of this object, which was originally stored in the symbol table.
- (int*) (&d) represents a pointer to the int type, but the starting address of the pointer is the same as the &d, except that the type has changed. The value of cout<< ((int*) (&d) output is the same as cout<<&d.
- * (int*) (&d) represents a pointer to a virtual function table that points to the starting address of a virtual function table,cout<< The result of this pointer is the number of the memory unit that the pointer points to. At the same time, the memory unit that stores this pointer variable is _vptr_derived, so the resulting output is stored in the Derived object, which is the value of _vptr_derived.
- (int*) * (int*) (&d) +1 Pointer to the second slot in the virtual function table The result of,cout<< is the house number that points to the inner deposit cell. This pointer is computed and no memory unit holds the value of the pointer variable.
- * ((int*) * (int*) (&d) +1) this pointer points to void Derived::p rint (void) const The function pointer of this function, but the result of cout this pointer is the number of the content pointed to by this pointer, But the value of this pointer actually holds the memory location that is the contents of the second slot in the virtual function table.
C + + object model 2--pointer cout results