C + + Learning _ inherit override overload

Source: Internet
Author: User

Today through the second inheritance of experiments, overloading, covering the learning, let me understand the difference of these concepts in a deeper step.

First, define a concept where the function name is the address, meaning that the function name is a pointer.

During the compile phase, the compiler assigns an address space to the code for each function and compiles the function code into this space, and the function name points to the address space.

That is, each function name has its own unique code space.

Similarly, the member functions of a class are the same.

However, it is important to remember that when the C + + compiler compiles CPP files, the function names are decorated according to the function name decoration rules of the C + + compiler.
(Modify the rules of the people themselves to search it, I do not describe), the previous function name is the function of the actual code to the pointer.
Knowing the above rules, it is not difficult for us to understand the function overlay.

First of all, to see the Baidu Encyclopedia function coverage of the Chinese description is:
Function overrides occur between the parent class and the subclass, and the function name, parameter type, and return value type must be strictly consistent with the function that corresponds to the override in the half class.
The override function and the overridden function are only different from the function body, and the overridden version in the subclass is automatically called when the derived class object calls the function with the same name in the child class.
Instead of the overridden version of the function in the parent class, this mechanism is called a function overlay.

Let's write a code that covers the function.
Class father
{
Public

void Fun ()
{cout<< "Father ' s Fun" <<ENDL;}

};

Class Son:public father
{
Public
void Fun ()
{cout<< "son's Fun" <<ENDL;}
};

void Main ()
{
Father Father,*pfather;
Son Son,*pson;
int i = sizeof (Father); After this line of code, I=1, this line of code is meaningless just to let everyone know that ordinary member functions do not occupy the class's instance space.

Father.fun (); This line of comments is normal for calling function overrides with the downlink comment. I'm sure we can understand that, so it's not explained.
Sun.fun ();
Pson = (son*) &Father; A subclass pointer to a parent class instance is dangerous, and this example does not involve any cross-border, and in order to show the difference
That is why it is so used, but we must understand that it is dangerous to do so.
Pfather = (father*) &Son;
Pson->fun (); The function call executed the father ' s fun
Pfather->fun (); Function call executed son's fun
}

At this point some people may not understand why this call results, then do you still remember the "C + + compiler's function name decoration rules" I mentioned above?
Let's think about the reason, based on the C + + compiler's function name decoration rules.
According to the rules, the compiler compiles the name of the fun function in the parent class father to "[email protected]@ @QAEZ", and the fun function name in the subclass son is compiled as "[email protected]@ @QAEZ".
When Pson->fun () is called, the compiler converts the type of the address value stored Pson to the current pointer type, and the current type of Pson is son (this is not redundant,
Because the type is free to convert),
So the expression "pson->fun ()" expands to get the function name "[e-mail protected]@ @QAEZ"
The same expression "pfather->fun ()" expands to get the function name "[Email protected]@ @QAEZ",
Now that the function name is obtained, it is possible to jump to the actual function code entity position according to the function names.


According to the above analysis, I think "function coverage (English name does not know what can be called only in Chinese)" This word is really easy to lead people astray,
My language is not good, so I also hope which language good brother, to re-translate the "function overlay" the word.
Well, really laborious ah, function coverage is finished, do not know everyone has to understand, if still can not understand, I really did not recruit.
Here's a virtual function.

With the previous foundation, you should have a good understanding of the function.
So ask you a question, why is a class after the strength of the ordinary member function does not affect the size of the instance?
Hehe, if a novice can answer, then I this article is not a white writing.
The correct answer is because it does not need to affect the instance size, because the compiler modifies the rule and the address type of the expression according to the function name of the C + + compiler.
The member function is automatically expanded into the full function name, and the real address of the function is found, so the normal member function does not affect the strength size.

Let me ask you one more question, do you think that virtual functions need not affect the instance size of the class?
Haha, this time the answer is need.
This time, let's write a code for a virtual function. Because you can only do this by adding a pointer inside the instance, for example,

Class father
{
Public
virtual void Fun ()
{cout<< "Father ' s Fun" <<ENDL;}

};

Class Son:public father
{
Public
void Fun ()
{cout<< "son's Fun" <<ENDL;}
};

void Main ()
{
Father Father,*pfather;
Son Son,*pson;
int i = sizeof (Father); After this line of code, i=4, this line of code is meaningless just to let you know the virtual function occupies the instance space of the class.

Father.fun (); Function execution result "father ' s Fun"
Son.fun (); function execution Results "Son's fun," The simple point is that many people say that dynamic binding, we will be specific analysis below.

Pson = (son*) &Father; Again, it is dangerous to point the subclass pointer to the parent class instance, and throws an exception once it crosses the line.
Pfather = (father*) &Son;
Pson->fun (); Function execution result "father ' s Fun"
Pfather->fun (); Function execution result "son's fun" these two sentences are also dynamic binding, I believe there are many people do not understand, the following specific analysis.
}

Well, you need to tune the above program, add monitoring to the father, you will find father instances in the inexplicably more than one vftable type pointer object vfptr.
Yes, the realization of virtual function depends on this thing.
The IED helped us instantiate an Vftable object outside our instance (I know it's a lot of things, but I don't know how to explain it better),
We also added a pointer vfptr to the Vftable object for our instance, father.
We continue to expand the pointer vfptr in the watch, and we can see a function pointer called [0] (Don't ask me why this is so, I don't know),
Haha, found, here store an address, this address is a real address of the function (if you use the VS2010 compiler, you will see this address visually
The corresponding is the Father::fun function).
Then, let me clarify a virtual function rule, that is, when your instance calls a virtual function, the final call is that the member of this vftable type [0] stores the address.
Well, we know that the subclass is fully inheriting the parent class, so the Vftable type pointer object vfptr is also inherited.
The IDE also instantiates a Vftable object for us, allowing Vfptr to point to the Vftable object.
And if our subclass overrides the virtual function, then the IDE, when instantiating the Vftable object, overrides the [0] pointer to the virtual function address in the new subclass.
If our subclass does not override this virtual function, then the IDE will find a parent class that implements the virtual function closest to the subclass relationship.
Writes the address of the virtual function in the parent class to [0] of the subclass.
In this way, a class can implement dynamic binding when it calls a virtual function.
In addition, when the parent pointer points to the subclass instance, because of the vfptr pointer placeholder, when the parent pointer calls the virtual function, the vfptr that is addressed to is the subclass instance.
The subclass's vfptr points to the subclass's own Vftable object, so the parent class will eventually call in [0] of the subclass object, so what function address is stored in [0].
Which function the parent pointer will eventually call.

C + + Learning _ inherit override overload

Related Article

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.