C + + It specifies the behavior of the virtual function, but implements the author compiler.
Typically, the compiler handles virtual functions by adding a hidden member to each object. A pointer to an array of function addresses is saved in the hidden member.
This array is called virtual function table,vtbl. The virtual function table stores the address of the virtual function declared for the class object.
For example: A base class object includes a pointer to the virtual function table of the base class.
A derived class object includes a pointer to a separate virtual function table. Assuming that the derived class provides a new definition of the virtual function, the virtual function table will save the new function address.
assuming that a derived class does not define a virtual function again, the virtual function table will hold the address of the original version number. Assuming that the derived class defines a new virtual function, the function address is added to the virtual function table as well !
Look at the following examples:
#include <iostream> #include <string>using namespace Std;class animal{protected:string name;public:animal (const string &s): Name (s) {}virtual ~animal () {}//non-virtual function void Eat () const{cout<< "Animal eat!" <<endl;} Virtual function not overridden virtual void run () const{cout<< "Animal run!" <<endl;} Virtual function that will be rewritten virtual void speak () const{cout<< "I ' m a animal!" <<endl;}}; Class Dog:public Animal{public:dog (const string &s): Animal (s) {}virtual ~dog () {}//A newly defined function eat, will obscure the old version number, not rewrite ( overriding refers to overriding the virtual function) void eat () const{cout<< "Dog eat!" <<endl;} Rewrite speak () virtual void speak () const override{cout<< "This ' s a dog!" <<endl;} The new virtual function virtual void fun1 () const{}};int main () {Animal A ("Animalone");D og d1 ("Dogone"); Animal *p1=&a; Animal *p2=&d1;p1->speak ();p 2->speak ();p 1->eat ();p 2->eat ();//call animal::eat () P1->run ();p 2- >run ();//call animal::run () Animal &r1=a; Animal &r2=d1;r1.speak (); R2.speak (); R1.eat (); R2.eat (); R1.run (); R2.run (); return 0;}
Note that eat is not a virtual function and is not stored in a virtual function table
//animal virtual function table Address:
Animal::run ( ) 4000
Animal::speak (5000)
Dog virtual function table Address:
dog::run () 4000 (not rewritten, save original address)
Dog::speak () 7000 (rewritten, save new address)
dog::fun () 8000 (New virtual function, save address)
Specifically analyze the following code
P1->speak (); //Find the address of speak in animal, 5000
P2->speak (); //Find the address of speak in dog, 7000, run code dog::speak ();
P1->run (); //Ibid.
P2->run (); //
In this way, for the virtual function table has a preliminary understanding !
Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.
C + + Learning Note 27, virtual function works