"C + +" Deep Exploration C + + object model reading note--function (the semantics of Function)

Source: Internet
Author: User

1. nonstatic the member function (non-static member function) Call method

  The compiler converts the "member function instance" to the equivalent "nonmember function instance".

For non-static member functions

float Const {...}

The conversion steps are as follows:

1. Rewrite the function's signature (meaning: function prototype) to place an additional parameter into the member function to provide an access pipeline so that class object can call this function. This extra parameter is called the This pointer:

// the expansion process of Non-const nonstatic member Point3D point3d::magnitude (Point3D *constThis) if the member function is const, it becomes:// the expansion process of the Const nonstatic member: Point3D point3d::magnitued (const Point3D *constThis)

2. Change each "Access operation to nonstatic data member" to be accessed via the this pointer:

3. Re-write the member function as an external one. The function name is treated "mangling" so that it becomes a unique word in the program:

extern MAGNITUDE_7POINT3DFV (    *constthis);

Now this function has been converted, and each invocation operation must be converted as well. So:

1 obj.magnitude (); 2 has become: 3     MAGNITUDE_7POINT3DFV (&obj); 4 and 5     Ptr->magnitued (); 6 has become: 7     MAGNITUDE_7POINT3DFV (PTR);

2. Virtual Member Functions (dummy member function)

To invoke a virtual function with a pointer:

If normalize () is a virtual member function, then the following call:

Ptr->normalize ();

will be converted internally into:

(*ptr->vptr[1]) (PTR);

which

    • Vptr represents a pointer generated by the compiler, pointing to Virtual table. It is placed in a class object that each "declares (or inherits from) one or more virtual functions"
    • 1 is the index value of the virtual table slot, which is associated to the normalize () function.
    • A second PTR represents the this pointer.

  

To invoke a virtual function using a class object:

  Use class objects to invoke virtual functions, which are parsed in the same way as non-static member functions. For the normalize () function in the previous section. The invocation method is eventually resolved to:

NORMALIZE_7POINT3DFV (&obj);

3. Static member function (statically member functions)

1. Key features of static member functions:

    • It cannot directly access the nonstatic members in class
    • It cannot be declared as cosnt, volatile, or virtual
    • It does not need to be called through class object--although it is called in most cases

2. Take the address of a static member function, and get the location in memory, that is, its address. Because the static member function does not have this pointer, its address type is not a "pointer to class member function", but rather a "nonmember function pointer"

"C + +" Deep Exploration C + + object model reading note--function (the semantics of Function)

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.