C ++ function matching and runtime type identification during compilation

Source: Internet
Author: User

Overload: two functions with the same name and different parameters under the same scope are overloaded functions;
Overwrite: the parent class's virtual function is rewritten in the subclass, and the parent class virtual function with the same name and parameter is overwritten;
Hide: a function in the parent class is called the fun (int, double) function. The function fun (xxx) in the subclass contains all functions in the parent class regardless of whether the parameters are the same, although you may think it is disgusting, Benjamin, the master of cxx, is in the safety consideration when designing cxx: Some c ++ programmers may not know the function with such a name in the parent class, A function with the same name is written in the subclass. It is not so accurate when passing the parameter. In this case, it is terrible to execute the function for the parent class, so the hidden cxx will appear later.
Let's talk about the function matching problem during the compilation period, for example, an object Derived d; such an object, d. test (); the compiler will find the function test with the same name in the Class d, and then the real parameter matches the form parameter. The entry address with the highest matching degree is, if two functions with the same matching degree are found, the compiler reports an error;
Eg:
Class Base {
Public:
Virtual int test () {cout <"base" <endl ;}
};
Class Derived: public Base {
Public:
Int test (int I) {cout <"derived: int" <endl ;}
Int test (double I) {cout <"derived: double" <endl ;}
};
Int main (){
Derived d;
D. test ();
}
D. test (); will make the compiler find the name test in the subclass, and find two test (int I) and test (double). Note: here we cannot find the test () function in the Base. The reason is that the above explanation is clear, when designing cxx, Master Benjamin explicitly refused to find functions with the same name in the base class, which caused some programmers to mistakenly think that the program is correct. Therefore, this sentence may cause errors in the cxx compiler;
If we rewrite the definition of Derived to the following form:
Class Derived: public Base {
Public:
Int test (int I) {cout <"derived: int" <endl ;}
Int test (double I) {cout <"derived: double" <endl ;}
Using Base: test;
};
In this case, the programmer tells the compiler that I want to find the matching function in the base class. At this time, the compiler will let you go, because there is no misunderstanding; at this time, d. test (); will find three matching functions, and the final matching degree is of course the one in the parent class, parameter matching; OK, compiled through, of course, if the test function in the parent class is set to private here, the compiler will still report an error because you use using Base: test to assume that the test in the parent class is pulic, it is reasonable for the compiler to report an error here. He tells you what you think is wrong;

The above is probably hidden. Next we will explain the remarkable coverage;
Overwrite, in fact, is defined differently in each place. here it means to redefine the virtual function in the parent class in the subclass;
First, we need to understand why the parent class designs virtual functions. In fact, the parent class designer knows that this function will be redefined. After being redefined, base * B = new Derived (); B's memory model is simple,
In fact, the virtual function table is generated during compilation. Why? Because the virtual function tables of each class are actually the same. After the table is generated during compilation, it can be directly put into the memory. Only one virtual function table is required for each class, every object in the running period only needs a 4-byte pointer to point to the virtual function table, saving space. That is to say, virtual functions are drawn during compilation. In other words, function overwrite is actually completed during compilation. The so-called runtime type recognition should be explained using the following example:
Base * pb = new Derived (); pb is a Base pointer, But it points to a derived object. Didn't you say that the derived object has a virtual function table pointer, this Pointer Points to the Derived-like virtual function table, that is, when running, pb will first find the derived object, and then find the corresponding virtual function table through this object, find the entry to the response function in the virtual function table and call the functions of the Derived class;
In this case, Base * pb = new Base (); pb points to a Base object, and finally finds the Base class virtual function table, and then the Base function. This achieves runtime type recognition;
Therefore, the benefit of generating a virtual function table during compilation is space saving. The first is a virtual function pointer pointing to a virtual function table. The first function in the virtual function table is test (), assuming that the test () function is redefined in Derived, the compiler will allow the test () function of Derived to overwrite the test () of the Base (), then, if the Base contains the second virtual function test2 (), it will be in a table after the Derived: test () of the virtual function table, and so on;
Note: The virtual function table of the parent class inherits from the quilt class and then overwrites it;

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.