Understanding concepts of virtual base classes, virtual functions, and pure virtual functions

Source: Internet
Author: User

Summary

Virtual base class
1. A class can be used as either a virtual base class or a non-virtual base class in a class family.
2. In a derived class object, a virtual base class with the same name only produces one virtual base class sub-object, while a non-virtual base class generates its own sub-object.
3. The virtual base class sub-object is initialized by the constructor of the most derived class by calling the constructor of the virtual base class.
4. The most derived class is the class specified when an object is created in the inheritance structure.
5. The member initialization list of the constructor of the derived class must list the calls to the constructor of the virtual base class. If not, the default constructor of the virtual base class is used.
6. The call to the virtual base class constructor must be listed in the constructor initialization list of the derived class directly or indirectly derived from the virtual base class. However, only constructors used to create the most derived class of an object call the constructors of the virtual base class, the call to the constructor of the virtual base class listed in all base classes of the derived class is ignored during execution, so as to ensure that the sub-objects of the virtual base class are initialized only once.
7. When you call both the virtual and non-virtual base constructors in the member initialization list, the constructors of the virtual base class are executed before the constructors of the non-virtual base class.

Virtual Functions
1. virtual functions are non-static, non-inline member functions, but not friend functions. However, virtual functions can be declared as friend functions in another class.
2. The virtual function declaration can only appear in the class-defined function prototype declaration, but cannot be declared when the function body of the member function is implemented.
3. No matter how many times a virtual function is inherited by the public, it still maintains the features of its virtual function.
4. If a member function in the class is described as a virtual function, the member function may have different implementations in the derived class. When this member function is used to operate a pointer or reference the identified object, dynamic association can be used for calling this member function.
5. After a virtual function is defined, the pointer declared in the program to the base class can point to its derived class. During execution, the function can constantly change the objects it points to, call member functions of different versions, and these actions are dynamically implemented at runtime. Virtual functions fully reflect the dynamic polymorphism of object-oriented programming. Pure virtual function version member functions, and these actions are dynamically implemented at runtime. Virtual functions fully reflect the dynamic polymorphism of object-oriented programming.

Pure virtual functions
1. When the base class cannot provide a meaningful implementation for the virtual function, you can declare it as a pure virtual function, and its implementation will be left for the completion of the derived class.
2. The pure virtual function provides a consistent interface for the derived class.
3. Pure virtual functions cannot be implemented, but pointers can be declared.

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.