Explore the C + + object model in Depth--fourth chapter-function semantics

Source: Internet
Author: User

Start sorting out the contents of chapter fourth, about functions.

1. Static member functions may not be const because of the this pointer, for detailed reference: http://blog.csdn.net/beyongwang/article/details/52403697

2. member functions can be grouped into several categories:

A. Non-static member functions: This type of function implicitly implies a this pointer passing in the function body, for example, for the following functions:

Float A::non_static_fun () {return
    m_a * M_A;
}

Call the steps as follows: Overwrite the function signature, add the this pointer, and for the const function, add the const * this:

Float A::non_static_fun (* this) {
    ...
}
Change the access of each non-static data member to be done by the this pointer:
Float A::non_static_fun (* this) {return
    this->m_a * this->m_a;
}
The member function is rewritten as an external function, the function name is mangling processing, the general processing method is the function name + number of parameters + parameter type, note that there is no return value, this is why in C + +, function overloading is not allowed by the return value to distinguish, (add extern The C keyword can block this mangling, making it a unique function name:
extern NON_STATIC_FUN_7AFV (...);
The corresponding invocation operation for this function is also converted:
A.non_static_fun ();
will be translated into
NON_STATIC_FUN_7AFV (&a);
Pointer calls:
Pa->non_static_fun ();

will be translated into:

NON_STATIC_FUN_7AFV (PA);

B. Virtual member functions: strictly speaking, virtual functions also belong to non-static member functions, they have some of the same processing, but due to the normal Non-static member function processing and different, so separate into a class. For example, if a virtual member function of Class A is v_fun (void), the call Pa->v_fun () may be turned into (* pa->vptr[1]) (PA), where the I vptr is the compiler-generated pointer to the virtual table. For all classes that contain or inherit virtual functions, their names may also be mangling, because there may be more than one such pointer in the same class.
Ii. 1 is the index value of the virtual table slot, indicating that this function is in the second function slot of the virtual table.

Iii. the second PA represents the this pointer. Note that using a class object to invoke a virtual function is the same as calling a non-virtual function, and does not involve a virtual table, for example: A.v_fun () translates to: V_FUN_7AFV (&a), which is the same as a non-static member function called above. This is because the type of the class object is OK, so you do not have to use the virtual function mechanism to determine the address of the called function.

c. Static member functions: the difference between a static member function and a non-static member function is that the former does not have the this pointer, because the former belongs to the entire class and does not store the particular class instance state because of the owning relationship, which may produce different results because of different instances. For example, for a static member function of Class A, void Static_fun (void), this call Pa->static_fun (), and this call: A.static_fun (), will be converted to a generic, non-member function call: Static_fun_ 7ASFv ();

Characteristics of several static functions: I. It cannot directly access non-static data members of the owning class.
II. It cannot be declared as const, volatile or virtual.

III. It does not need to be invoked through class instance objects, A::static_fun ().

The address of the static member function: &a::static_fun (), where it is located in memory, which is a normal function pointer: void (*) () instead of Pointer Void (a::*) that points to a class member function (a pointer to a class member function). A bit like a pointer to a class member variable, and then there is a collation.

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.