2008-09-19 20:18

     stay C++ in , Polymorphism is realized by virtual function .

Base class if a function is declared as virtual (virtual), It means that inherited classes can override (override) this
function ( So as to show different behaviors , Showing polymorphism ).

For every concrete class with virtual function ( Or inheritance class ), It can be considered that there is a virtual function table associated with it (v-t
able).v-table Each item in the table (slot) The appropriate function pointer is stored in .C++ Compiler compiling
All necessary virtual function tables are created at all times . also , The entries in each virtual function table have been filled with appropriate values (
Points to the right function entry ).

for example , There are three classes below :Base,Derived,Derived2.

class Base
{
     virtual void f1() {}
     virtual void f2() {}
     virtual void f3() {}
}

Base The virtual function table of class is as follows .
     slot1 – Base::f1()
     slot2 – Base::f2()
     slot3 – Base::f3()

class Derived: public Base
{
    //void f1() {}
    void f2() {}
    virtual void f3() {}
    virtual void f4() {}
    virtual void f5() {}
}

Derived Two items are added to the virtual function table of class ( The front section must be consistent with the Base The virtual function table is identical , include
Number and position order of table items ), as follows .
     slot1 – Base::f1()
     slot2 – Derived::f2()
     slot3 – Derived::f3()
     slot4 – Derived::f4()
     slot5 – Derived::f5()
be careful , I intend to Derived Not covered in class f1(), In this way, the table items 1 Point is still stored in Base::f1(
) Function pointer of . If Derived Cover in class f1() The words , Then table item 1 Point to Derived::f1().

class Derived2: public Derived
{
    void f3() {}
    void f4() {}
    void f5() {}
    virtual void f6() {}
    virtual void f7() {}
}

Derived2 Two more entries are added to the virtual function table of class , as follows .
     slot1 – Base::f1()
     slot2 – Derived::f2()
     slot3 – Derived2::f3()
     slot4 – Derived2::f4()
     slot5 – Derived2::f5()
     slot6 – Derived2::f6()
     slot7 – Derived2::f7()

Now describe the call to the virtual function . In fact, we can see that , Indirect call for virtual function , because
It's done through the virtual function table . Imagine an object pointer a, call a A function of f(), If f() no
virtual function , So it's called directly ( In case of no ambiguity , There is no need to use the indirect call technique ,
Reduce efficiency ). If it's a virtual function , Then it must be through an item in the virtual function table ( Which table item is
The compilation phase is fully determined ), Initiate call . Why not call it directly ? Because the compiler is not sure
individual a The object pointed to is Base Examples of , still Derived Examples of ? You know, just one Derived Examples of , also
Can be molded by (downcast) Be treated as one Base example . And in Derived Class , function f() completely
May be covered ( Therefore, the corresponding entries in the virtual function table are overwritten , point Derived::f()). So in order to produce
Make the right call , Behave correctly , Must be called indirectly through the virtual function table . The whole process seems to change
Magic , But it also embodies the charm of polymorphism .

The definition of virtual function should follow the following important rules :

  1. If the virtual function appears in the base class and the derived class , Just the same name , But the formal parameters are different , Or different return types , So even if we add virtual keyword , There will be no lag binding .

  2. Only the member function of a class can be described as a virtual function , Because virtual functions are only suitable for class objects with inheritance relationship , So ordinary functions cannot be described as virtual functions .

  3. A static member function cannot be a virtual function , Because the characteristic of static member function is not limited to an object .

  The code is as follows :

  4. inline (inline) Function cannot be a virtual function , Because inline functions can't be dynamically located at runtime . Even if a virtual function is defined inside a class , But at compile time, the system still regards it as non inline .

  5. Constructor cannot be a virtual function , Because when it's constructed , Object is also a piece of space for bit shaping , Only after construction , Object is the instance of concrete class .

  6. Destructors can be virtual functions , And it's usually called a virtual function .

   Explain , Although we say that using virtual functions will reduce efficiency , But in today's faster and faster processors , Define all member functions in a class as virtual It's always good , It has no harm but to add some extra expenses , It's good for ensuring the encapsulation of classes .

   Important rules for the use of virtual functions above 6, It is necessary for us to give an example , Why the destructor of a class with polymorphism , It is necessary to declare as virtual.
================================================

C++ Precautions for calling pointer function of parent class and child class ( Virtual function and Polytype Polymorphism)
1, If you point to a derived class object with a base class pointer , Only the functions defined by the base class can be accessed through this pointer
2, If you point to a base class object with a derived class pointer , We must do the compulsory transformation first (explicit
cast), It's dangerous , It's not in line with living habits , It will also bring troubles to programmers in programming .
3, If a member function with the same name is defined in the base class and the derived class , When a member function is called through an object pointer , Which function to call depends on the prototype of the pointer , Not based on the type of object the pointer actually points to .
Virtual function is for “ If you point to a derived class object with a base class pointer , Then through the pointer , You can only access member functions defined by the underlying class ” The design of the rule against the other way .
If you expect a derived class to have a member function redefined by , So you define it as a virtual function ( virtual ).
polymorphism It means that the program code dealing with the basic class objects can continue to deal with the derived class objects properly . Pure virtual function : virtual void myfunc ( )
=0;
Pure virtual function cannot define its specific action , It exists only to be redefined in derived clocks . As long as it's a class with pure virtual functions , It's an abstract class , They cannot be instantiated . If an inherited class does not overwrite the pure virtual function in the parent class , So he's also an abstract class , Can't be instantiated .
Abstract class cannot be instantiated , But we can have pointers to abstract classes , To facilitate manipulation of various derived classes . The derivative of virtual function is still virtual function , You can also omit keywords “virtual”.

Technology
©2019-2020 Toolsou All rights reserved,
Python Basic knowledge and notes 2021 year 1 Monthly programmer salary statistics , average 14915 element GDOI2019 travels C++ Standard library What should I do if I suddenly encounter a question I can't answer during the interview ?2021 year 2 Monthly programmer salary statistics , average 15144 element college examination for the self-taught An overview of Marxism use C++ I want to talk to you “ Prototype mode ” ( copy / copy constructor ) How to use it quickly html and css Write static page Bitcoin in ten years ,VDS Opportunity or fraud