My understanding of polymorphism and heavy load and rewriting

Source: Internet
Author: User
The same statement has multiple forms, and different types have different attributes and presentation methods; polymorphism principle; polymorphism is a correspondence pointer.

Implementation of Polymorphism

Polymorphism: The same invocation statement has many different forms;

Three conditions for Polymorphism implementation

There are inheritance, virtual rewriting, and parent class pointers (references) pointing to subclass objects.

Multi-State C ++ implementation

The virtual keyword tells the compiler that the function must support polymorphism. Do not judge how to call the function based on the pointer type. Instead, determine how to call the function based on the actual object type pointed to by the pointer.
Theoretical Basis of Polymorphism

Dynamic Association and PK static Association. Judge the calling of the rewrite function based on the actual object type.

Significance of Polymorphism

The basis of the design pattern.

Theoretical Basis for realizing Polymorphism

Function pointers are used as function parameters.

Implementation principle of polymorphism in C ++

When a virtual function is declared in the class, the compiler generates a virtual function table in the class.

A virtual function table is a data structure of a function pointer for storing class members.

The virtual function table is automatically generated and maintained by the compiler.

Virtual member functions are written into the virtual function table by the compiler.

When a virtual function exists, each object has a pointer to the virtual function table (vptr pointer)




Note 1:

The virtual function table pointer vptr is used to call the rewrite function when the program is running. Therefore, the addressing operation is required to determine the function to be called. While normal member functions determine the called functions at compilation. In terms of efficiency, the efficiency of virtual functions is much lower.

NOTE 2:

For efficiency, it is not necessary to declare all member functions as virtual functions.

 

 

2,Can a constructor call a virtual function to realize polymorphism? Why?

1)When will the vptr pointer in the object be initialized?

 

When an object is created, the vptr pointer is initialized by the compiler.

The vptr orientation is final only when the object construction is complete.

The vptr of the parent class object points to the parent class virtual function table.

The vptr of the subclass object points to the virtual function table of the subclass.

 

2) analysis process

Graph Analysis

3) Conclusion: Polymorphism cannot be achieved when the constructor calls the polymorphism function.

4) how to prove the existence of the vptr pointer?



30. What does function overload mean? What is the difference between it and the concept of virtual functions? A: function Overloading is a function with the same name to complete different functions. during compilation, the compilation system determines which function to call based on the number and type of function parameters and the return value of the function, that is, static polymorphism is implemented. But remember: function overloading cannot be implemented only by different function return values. Virtual functions use the keyword virtual in the base class to declare a function as a virtual function, the meaning is that the function may be defined in the future derived class or extended based on the base class. The system can dynamically decide which function to call only in the running stage, therefore, dynamic polymorphism is implemented. It embodies a vertical concept, that is, implementation between the base class and the derived class.

45. What is the difference between a virtual function and a common member function? Can inline and constructor be virtual functions? Answer: difference: virtual functions have virtual keywords, virtual pointers, and virtual function tables. Virtual pointers are interfaces of virtual functions, but common member functions do not. Inline and constructor functions cannot be virtual functions.
46. How do I call constructor and destructor? Why is destructor virtualized? Answer: The Calling sequence of constructor: base class constructor-object member constructor-derived class constructor. The Calling sequence of the constructor is opposite to that of the constructor. The virtual destructor is used to prevent memory leakage caused by incomplete destructor.



127. differences between overload, overwrite, and hiding of class member functions answer: feature of member functions being overloaded: (1) Same range (in the same class); (2) Same function name; (3) Different parameters; (4) virtual keywords are optional. Override refers to the function of a derived class that overrides a base class function. The features are as follows: (1) different ranges (respectively located in the derived class and the base class); (2) the function name is the same; (3) the parameter is the same; (4) basic functions must have virtual keywords. "Hide" means that the function of the derived class shields the base class function with the same name. The rule is as follows: (1) if the function of the derived class has the same name as the base class function, but the parameter is different. In this case, functions of the base class will be hidden regardless of whether there is any virtual keyword (Be sure not to confuse them with overload ). (2) 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. At this time, the base class functions are hidden (Be sure not to confuse with overwrite)



Override PK overload to understand that the function overload must be performed in the same class as the subclass. The function of the parent class cannot be reloaded, A function with the same name as a parent class overwrites the name. during compilation, function calling is decided based on the parameter type and number. Function rewriting must take place between the parent class and the subclass, and the function in the parent class and the subclass must have completely the same prototype can produce polymorphism after using the virtual mission, if virtual is not applicable, the redefinition of polymorphism is to determine the function call according to the specific object type during the runtime.

My understanding of polymorphism and heavy load and rewriting

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.