C ++ virtual functions, functions

Source: Internet
Author: User

C ++ virtual functions, functions

C ++

A member function declared as virtual in a base class and redefined in one or more Derived classes. Its format is: virtual function return type function name (parameter table) {function body}; implements polymorphism. by pointing to the base class pointer or reference of the derived class, access the member function with the same name in the derived class to overwrite the member function. Simply put, member functions modified by virtual keywords are virtual functions. The role of virtual functions is explained in professional terms by implementing Polymorphism. Polymorphism separates interfaces from implementations. interpreting with image languages means implementing a common approach, however, due to individual differences, different strategies are adopted. -- (Come to Baidu encyclopedia)

The following are the defined test model classes:

1 class Base 2 {3 public: 4 virtual void print () 5 {6 printf ("Base print .. \ n "); 7} 8 9 void echo () 10 {11 printf (" Base echo .. \ n "); 12} 13 14 void doAction (int count) 15 {16 printf (" Base doAction: % d .. \ n ", count); 17} 18 ~ Base () 19 {20 printf ("Base :~ Base \ n "); 21} 22}; 23 24 class A: public Base25 {26 public: 27 void print () 28 {29 printf (" A print .. \ n "); 30} 31 32 void echo () 33 {34 printf (" A echo .. \ n "); 35} 36 37 void doAction (int count) 38 {39 printf (" A doAction: % d .. \ n ", count); 40} 41 ~ A () 42 {43 printf (":~ A \ n "); 44} 45 };Class Definition

In the following example

 1 int main(int argc, const char * argv[]) { 2     // insert code here... 3      4     A var; 5     Base *p = &var; 6     A *q = &var; 7      8     p->print(); 9     q->print();10     p->echo();11     q->echo();12     13     return 0;14 }

The output is as follows:

1 A print..2 A print..3 Base echo..4 A echo..

In the Base of the parent class, print () is set to A virtual function. The print () parameter of the same name as subclass A and the returned value are also set to the virtual function (paiaul void print () by default (), this step is automatically implemented by the compiler). The Compiler creates A virtual function table for A and Base respectively, and obtains the code segment address of the function by checking this table during function calling, and then execute different functions. Echo () in the two classes directly executes the corresponding code by obtaining the relative address through the pointer.

C ++ uses the default "hide" function to hide echo () in the parent Base, and echo () in subclass A is executed. Note that echo () is different from override.

Also, destructor are often defined as virtual

The above shows the forced conversion of child classes to parent classes. Now let's take a look at the forced conversion of child classes to child classes.

1 Base B; 2 A * a = (A *) & B; 3 4 a-> print (); // output Base print .. 5 a-> echo (); // output A echo .. 6 7 // compare 8 A a0; 9 Base * b0 = & a0; 10 b0-> print (); // output A print .. 11 b0-> echo (); // output Base echo ..

 

 

The output result may be different from the Expected One, especially the print () output. This requires the application mentioned in this article. The access to virtual functions is run in the form of virtual tables. pointer type conversion,Not AffectedVirtual table pointer.

Create a virtual table and initialize a virtual table pointerIn the constructor. However, in this example, only the constructor of the parent class is called, that is, only the virtual table of the parent class is initialized. the pointer type conversion does not call the constructor of the subclass.

 

Reference: 1. virtual functions, virtual pointers, and virtual tables 2. virtual functions 3. C ++ Polymorphism

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.