C + + object model 2--pointer cout results

Source: Internet
Author: User

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:

    1. &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.
    2. (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.
    3. * (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.
    4. (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.
    5. * ((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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.