Overload, multi-state virtual function

Source: Internet
Author: User

I am a beginner in C ++. I think this part of virtual functions is profound and profound. C ++ implements polymorphism through virtual functions. In C ++, the function that starts with the virtual keyword is a virtual function, and the virtual function is a function that the base class wants the derived class to be redefined, if you do not want the derived class to be redefined but inherit completely, do not define it as a virtual function. Once the function is declared as a virtual function in the base class, the fact that the derived class cannot be changed. When the virtual function is redefined in the derived class, the keyword virtual is dispensable.

Dynamic binding of a virtual function must satisfy two conditions at the same time: 1) this function is a virtual function. 2) The function must be called through a reference or pointer of the base class type. First, you need to know the fact that the object of each derived class contains the base class and there is a conversion from the derived class to the base class, that is, you can use pointers and references of the base class type to reference the objects of the derived class. Therefore, when a base class type pointer or reference is used, in fact, we cannot immediately confirm whether the bound object type is a base class or a derived class. It can only be determined at runtime. When you use a base class object to call a virtual function, you can call the version of the virtual function in the base class. When you use a derived class, you can call a version that is redefined in the derived class. When you want to overcome the virtual function mechanism, you can use the scope operator to display constraints.

From study to interview, I summarized several questions about virtual functions, as shown below:

1. virtual function table;

2. Can constructor be a virtual function;

3. Can static member functions be virtual functions;

4. Why should I use virtual functions in C ++ for virtual functions and overloading;

5. Pure virtual functions

To understand the 2.3 problem, first understand the concept and working principle of the virtual function table. I have read some materials before, but it is not very clear. I found a blog written by Daniel, which has benefited a lot:


Then let's take a look at the answers to the following questions.

2. Can constructor be a virtual function:

Answer:

1) from the knowledge above, we can see that the execution of virtual functions is implemented through the virtual function table. Therefore, the virtual function table must be initialized before the virtual function is called. The constructor must be executed to generate a class object, and VPTR setting is also completed in the constructor. In fact, before running a virtual function, the object type should be complete. Before the constructor is executed, the object type is incomplete. If the constructor is also a virtual function, no one will initialize the VPTR, and the virtual constructor cannot be executed for an incomplete type object. 2) imagine that a virtual function is a function in the base class that is overwritten in the subclass. If the constructor is a virtual function, isn't the constructor name in the derived class the name of the base class? This is obviously unreasonable.

3. Can static member functions be virtual functions:

Answer:

In the principle of virtual function tables, the compiler secretly inserts a VPTR in each type object, which points to the virtual function table. When executing a virtual member function, you can use this hidden object to determine the type of the function, and then call the function of the correct version through the virtual function table. The static member function does not have the this pointer, And it is static. After the subclass is inherited, it is still the original static function and does not show polymorphism. Therefore, the static function cannot be a virtual function.

4.
Overload selects the version of the function to be called Based on the parameter list of the function, while polymorphism selects the virtual function version to be called based on the actual type of the runtime object, the implementation of polymorphism is implemented by overwriting override for the virtual functions of the base class in the derived class. If the virtual functions of the base class are not overwritten by the derived class, the derived class automatically inherits the virtual function version of the base class. At this time, the virtual function of the base class version is called no matter whether the base class Pointer Points to the object is a base type or a derived type; if the derived class overwrites the override of the virtual function of the base class, the virtual function version to be called is selected based on the actual type of the object at runtime, for example, if the base class Pointer Points to an object of the derived type, the virtual function version of the derived class is called to realize polymorphism.

The purpose of using polymorphism is to declare the function as virtual in the base class and overwrite the virtual function version of the base class in the derived class, at this time, the function prototype is consistent with the base class, that is, the same parameter type with the same name. If you add a new function version to a derived class, you cannot dynamically call the new function version of the derived class through the base class pointer, this new function version is only used as an overloaded version of the derived class. In the same sentence, the overload is only valid in the current class, no matter whether you are overloaded in the base class or in the derived class, the two are not correlated.

The heavy load is static and the polymorphism is dynamic. Further, the overload is irrelevant to the object type actually pointed to by the pointer, And the polymorphism is related to the object type actually pointed to by the pointer. If the base class pointer calls the overloaded version of the derived class, the C ++ compiler considers it illegal. The C ++ compiler only considers that the base class pointer can only call the overloaded version of the base class, the overload is only valid within the namespace scope of the current class. inheritance will lose the feature of overloading. Of course, if the base class pointer calls a virtual function at this time, then, it dynamically selects the virtual function version of the base class or the virtual function version of the derived class for specific operations, this is determined by the object type actually pointed to by the base class pointer. Therefore, overloading is irrelevant to the object type actually pointed to by the pointer, and polymorphism is related to the object type actually pointed to by the pointer.

Here "hide" means that the function of the derived class shields the base class function with the same name as it. The specific rules are also summarized:

If the function of the derived class has the same name as the function of the base class, but the parameter is different. If the base class does not have the virtual keyword, the function of the base class will be hidden. (Be careful not to confuse with overload. Although the function name is the same as the parameter, it should be called overload, but it cannot be considered as overload here, because the derived class and the base class are not in the same namespace scope. It is understood as "hidden)

If the function of the derived class has the same name as the function of the base class, but the parameter is different. In this case, if the base class has the virtual keyword, the function of the base class will be implicitly inherited to the vtable of the derived class. In this case, the function in the derived class vtable refers to the function address of the base class version. At the same time, this new function version is added to the derived class as the overloaded version of the derived class. However, when the base class pointer implements the multi-state call function method, the new function version of the derived class will be hidden.

If the function of the derived class has the same name and parameter as the function of the base class, but the base class function does not have the virtual keyword. In this case, the base class functions are hidden. (Do not confuse with overwrite. Here it is understood as hidden ).

If the function of the derived class has the same name and parameter as the function of the base class, but the base class function has the virtual keyword. In this case, the base class functions are not "hidden ". (Here, you need to understand it as overwrite ).

5. Pure virtual functions

Sometimes, a base class is only used as an interface of its derived class, and you do not want to create an object of the base class. In this case, you can add at least one pure virtual function to the base class to make it an abstract base class. Add = 0 to the end of a common virtual function. when a derived class inherits an abstract base class, all pure virtual functions must be implemented. Otherwise, the derived class is still an abstract class and cannot create objects.

Author: "thousands of miles"

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.