Talking about the virtual functions inherited in c ++

Source: Internet
Author: User

Virtual functions in c ++ inheritance 

C ++ is an obvious manifestation of an object-oriented programming language. It supports the Inheritance Mechanism. There are many inheritance types in c ++, there are different classification methods based on different categories. For example, the number of base classes can be divided into multi-inheritance and single-inheritance, and the access permission can be divided into public inheritance, protected inheritance, and private inheritance, virtual inheritance and non-virtual inheritance can be divided into virtual inheritance and non-virtual inheritance. Of course, the classification standards here are overlapping parts. For example, non-virtual inheritance can be divided into single inheritance and multi-inheritance. We will discuss the virtual functions here, so we will discuss and analyze them from the virtual and non-virtual inheritance aspects.

1. non-virtual inheritance: single inheritance and multi-inheritance. Let's look at an example:
 1 class base1 2 { 3 public: 4  virtual void f(){} 5   virtual void h(){} 6 }; 7 class base2 8 { 9 public:10  virtual void g(){}11 };12 class derive1:public base113 {14 public:15 virtual void f(){}16 virtual void f1(){}17 }18 class derive2:public base1,public base219 {20 public:21  virtual void f(){}22  virtual void g(){}23  virtual void g1(){}24 };

 

Create four objects: base1 b1; base2 b2; derive1 d1; derive2 d2; learn the virtual function table in the dashboard of c ++, the following table lists the virtual functions of each class in the inheritance system. The virtual functions of base1 are as follows: vptr --> base1: f base1 :: h // vptr is the pointer to the virtual function table of the class. --> indicates that it points to a table. The following content is the virtual function table of base2 in the table: the virtual function table of vptr --> base2: g // derive1 is as follows: vptr --> derive1: f base1: h derive1: fderive2: vptr1 --> derive2: f base1: h derive2: g1 // base1vptr2 --> derive2: g // base2 view the following table of virtual functions on the windows Development Platform: the host environment is win7 + vs2013 (not completely displayed). In debugging mode: 2. virtual Replication Virtual inheritance is a technology introduced in Multi-inheritance. The analysis and discussion of virtual inheritance is not the focus of this article. If you do not know it, you can view related c ++ books, the following describes virtual functions in virtual. 1 class base1 2 {3 public: 4 virtual void f () {} 5}; 6 class base2 7 {8 public: 9 virtual void g () {} 10 }; 11 class derive1: virtual public base112 {13 public: 14 virtual void f () {} 15 virtual void f1 () {} 16} 17 class derive2: virtual public base1, virtual public base218 {19 public: 20 virtual void f () {}21 virtual void g () {}22 virtual void g1 () {}23 };View Code

 

Through vs2013 to view the virtual function table is as follows: This article refer to the well-known technology blogger left ears mouse in the csdn blog, blog address: http://blog.csdn.net/haoel/article/details/1948051 welcome everyone to discuss and exchange, welcome to reprint, do not use it for commercial purposes. All Rights Reserved. The source must be indicated for reprinting.

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.