C++
A member function declared in a base class as virtual and redefined in one or more derived classes: The virtual function returns the type function name (parameter table) {function Body}, implements polymorphism, and accesses the overridden member function with the same name in the derived class by pointing to the base class pointer or reference of the derived class. Simply put, those member functions that are modified by the virtual keyword are virtual functions. The function of virtual function, the use of professional terminology to explain is to achieve polymorphism (polymorphism), polymorphism is the interface and implementation of the separation, with the image of the language to be realized in a common way, but because of individual differences, and adopt different strategies. --(Come to Baidu Encyclopedia)
The following are the defined test model classes:
1 classBase2 {3 Public:4 Virtual voidprint ()5 {6printf"Base Print: \ n");7 }8 9 voidEcho ()Ten { Oneprintf"Base Echo. \ n"); A } - - voidDoAction (intcount) the { -printf"Base doaction:%d. \ n", count); - } -~Base () + { -printf"Base: ~base\n"); + } A }; at - classA: PublicBase - { - Public: - voidprint () - { inprintf"A Print: \ n"); - } to + voidEcho () - { theprintf"A Echo. \ n"); * } $ Panax Notoginseng voidDoAction (intcount) - { theprintf"A doaction:%d. \ n", count); + } A~A () the { +printf"A: ~a\n"); - } $};class definition
The following code example is for multiple
1 intMainintargcConst Char*argv[]) {2 //Insert code here ...3 4Avar;5Base *p = &var;6A *q = &var;7 8P->print ();9Q->print ();TenP->Echo (); OneQ->Echo (); A - return 0; -}
The output is as follows:
1 A Print: 2 A Print: 3 Base Echo. 4 A echo.
In the parent class base, print () is set to virtual function, subclass a with the same name as the same parameter, and print () of the return value defaults to the virtual function (Virtaul void print (), which the compiler automatically does for us), and the compiler sets up a virtual function table for a and base respectively. When the function is called, the code snippet address of the function is obtained by checking the table to perform different functions. Echo () in two classes executes the corresponding code directly by obtaining a relative address from a pointer.
C + + default "Hide" function, so that echo () in the parent class base is hidden, and the Echo () in subclass A is executed, please note that unlike the override
Another: Destructors are often also defined as virtual
The above renders a case where the subclass is coerced into a parent class, now look at the case where the parent class is coerced into the subclass.
1 Base B;2A *a = (A *) &b;3 4A->print ();//Output Base Print :5A->echo ();//output a echo.6 7 //Compare8 A A0;9Base *b0 = &A0;TenB0->print ();//output a print. OneB0->echo ();//Output base echo:
This output may not be the same as expected, especially the output of print (). This needs to be applied this article has begun to mention that virtual function access is run in the form of a virtual table, pointer type conversion, does not affect the virtual table pointer .
The creation of virtual tables and the initialization of virtual table pointers are performed in constructors, but in this case only the constructor of the parent class is called, that is, only the virtual table of the parent class is initialized, and the pointer type conversion does not invoke the constructor of the subclass.
Reference: 1, virtual function, virtual pointer and virtual table 2, virtual function 3, Elementary Introduction to C + + polymorphism
C + + virtual functions