C + + Learning Notes (2)---2.5 C + + function compiling principle and implementation of member function

Source: Internet
Author: User

Reprinted from: http://c.biancheng.NET/cpp/biancheng/view/2996.html Click to open link

As can be seen from the example in the previous section, the object's memory model retains only the member variables, except that there is no other information, the program runs without knowing that the type of obj is Demo, and that it has a member function display (). So, how do you call member functions by object?

Compilation of C + + functions

C + + and C languages are compiled in different ways. The functions in the C language do not change at compile time, or simply add an underscore _ (different compilers have different implementations), for example, Func () after compiling to Func () or _func ().

The functions in C + + are renamed according to the namespace, class, parameter signature and other information at compile time to form a new function name. This renaming process is implemented by a special algorithm called the name Code (named mangling).

Name mangling is a reversible algorithm that can either compute a new function name from an existing function name, or reverse the original function name by a new functional name.

Name mangling ensures the uniqueness of the new function name, as long as there is a difference between the namespace, the owning class, the parameter signature, and so on, the new function name will be different.

If you want to see the new function name generated by the algorithm, you can simply declare and not define the function, so the call to the function will produce a link error, from the error message can be seen. Take a look at the following code:

[CPP]View PlainCopy
  1. #include <iostream>
  2. Using namespace std;
  3. void display ();
  4. void display (int);
  5. Namespace ns{
  6. void display ();
  7. }
  8. Class demo{
  9. Public
  10. void display ();
  11. };
  12. int main () {
  13. Display ();
  14. Display (1);
  15. NS::d isplay ();
  16. Demo obj;
  17. Obj.display ();
  18. return 0;
  19. }


In this example, four functions with the same name are declared, including two global functions with overloaded relationships, a function under the namespace NS, and a function that belongs to the class Demo. They are all functions that are declared but not defined.

Compile the source code to see the error message:


The parentheses are the names of the new functions generated by name mangling, all with "?" To distinguish the "_" in the C language.

Is the error message generated by VS2010, different compilers have different name mangling algorithms, and the resulting function names are not the same.

__thiscall, cdecl is a function call method, interested readers can bash "several functions of the method of invocation" of the article in-depth understanding.

In addition to functions, some variables are also generated by the name mangling algorithm to create a new name, no longer repeat.

Invocation of member functions

As can be seen, the member function is eventually compiled into an object-independent normal function, if there is no member variable in the function body, then the problem is very simple, do not have to do any processing of functions, directly call.

What if a member variable is used in a member function? The scope of the member variable is not global and cannot be accessed inside the function without any processing.

C + + stipulates that when compiling a member function, an additional parameter is added, passing the pointer to the current object and accessing the member variable through a pointer.

Suppose the Demo class has two member variables of type int, respectively A and B, and is used in the member function display () as follows:

[CPP]View PlainCopy
    1. void Demo::d isplay () {
    2. cout<<a<<endl;
    3. cout<<b<<endl;
    4. }


Then the compiled form resembles the following:

[CPP]View PlainCopy
    1. void New_function_name (const Demo *p) {
    2. //Use pointer p to access a, B
    3. cout<<p->a<<endl;
    4. cout<<p->b<<endl;
    5. }


Called when the form is similar to:

[CPP]View PlainCopy
    1. New_function_name (&obj);


This completes the association of the object and the member function, but instead of looking at the function through the object, we can find the object through the function rather than what we see from the show.

All of this is done implicitly, completely transparent to the programmer, as if this extra parameter doesn't exist.

C + + Learning Notes (2)---2.5 C + + function compiling principle and implementation of member 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.