Virtual inheritance and virtual function inheritance

Source: Internet
Author: User

Virtual inheritance and virtual function inheritance

Virtual inheritance is mainly used for inheritance in the form of Diamond

Virtual inheritance is used to avoid ambiguity during multi-inheritance,
For example, Class A has a, B inherits A, and C also inherits A. When D inherits B and C, ambiguity arises, therefore, you must use virtual inheritance to avoid duplicate copies.
Virtual function inheritance solves polymorphism. When a base class Pointer Points to a derived class object, the base class pointer automatically calls the virtual function of the derived class when calling the virtual function. This is polymorphism, also called dynamic compilation

Inheritance of virtual functions:
Class
{
Virtual void fun () {cout <'A' <endl ;};
};

Class B: public
{
Virtual void fun () {cout <'B' <endl ;};
};

Int main (int argv, char ** argc)
{
A * p = new B;
P-> fun (); // result output B, not A. As for the Implementation principle, the object header contains four more bytes, which is an address pointer to the virtual function table, when the program is running, find the entry address of B: fun () in this table.
Return 0;
}

In general, virtual inheritance is designed to save memory. It is a unique concept in multiple inheritance. Applicable to the diamond inheritance form.
For example, both class B and class C inherit Class A and Class D inherit class B and class C. To save memory space, B and C can inherit from A as virtual inheritance. In this case, A becomes A virtual base class.
Class;
Class B: vitual public;
Class C: vitual public;
Class D: public B, public C;

Virtual function inheritance means overwriting. That is, the virtual functions in the base class are overwritten by functions with the same name in the derived class.
Class parent
{
Public:
Vitual void foo () {cout <"foo from parent ";};
Void foo1 () {cout <"foo1 from parent ";};
};
Class son: public parent
{
Void foo () {cout <"foo from son ";};
Void foo1 () {cout <"foo1 from son ";};
};
Int main ()
{
Parent * p = new son ();
P-> foo ();
P-> foo1 ();
Return 0;
}

The output result is:
Foo from son, foo1 from parent

 

1. in the real sense, virtual function calls are bound at runtime;
2. What is a real virtual function call? Execute virtual functions through pointers or references;
3. Will virtual functions executed through objects be dynamically bound? No.
4. Whether a class has a virtual function depends on whether it contains a pointer to the virtual function table;
5. If the class itself contains a virtual declared function or inherits the virtual function, it will certainly contain a pointer to the virtual function table;
6. inheriting virutal from a pure or non-abstract class is the same in the sense and the efficiency is the same, not because you are inheriting a pure abstract class and the efficiency is higher;
7. How slow is a virtual function call than a common function call? Assume that this function only executes return I> j, which is about 15% slow (30 million * 100 scale tests). If it is a meaningful function, the effect of efficiency is negligible;
8. Therefore, the slow speed of virtual functions is basically fart, and it is worrying about the impact of virtual functions on efficiency;
9. virtual functions are slow, but they are for inline functions. virtual functions ignore the inline prefix;
10. The Inheritance level does not affect the efficiency of the virtual function. If your class is inherited from the first layer of the original class, there is no difference between the calling efficiency of the virtual function and the class inherited from the second layer, of course, if you want to call the above layer of virtual functions in this function, it will be another matter;
11. Each class should have only one virtual table, rather than each object (the object only contains a pointer to the virtual table), which means that the virtual function can increase the space overhead;
12. If a class contains virtual functions, using memset (this, 0, sizeof (* this) during construction is an endless action;
13. virtual functions are run-time polymorphism, templates are compiled-time polymorphism, one dynamic, and the other static.

14. When the subclass overwrites the virtual function of the parent class, the function pointer in the virtual table is actually modified in the constructor; therefore, the FatherClass * p = new ChildClass (); in this case, p-> VirtualFunc () always executes the virtual function of the subclass;

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.