Typedef int (* healthcalcfunc) (const gamecharacter &)
When a member function is declared as a virtual function, all functions of the same name in the derived class automatically become virtual functions. Therefore, when the subclass declares the new virtual function, it can be added or not, but it is used to adding virtual to each layer of declared functions to make the program clearer.
The static functions of the constructor cannot be modified with the virtual keyword;
Common member functions and destructor can be modified with the virtual keyword;
Effects of virtual functions
The base class pointer is used to point to the inherited class object, and then the member function of the inherited class can be used.
Class base
{
Public: Base (){}
Public:
Virtual void print () {cout <"base" ;}// you can leave it empty.
};
Class derived: public Base
{
Public: derived (){}
Public:
Void print () {cout <"derived ";}
};
Int main ()
{
Base * point = new derived ();
Point-> Print ();
}
Output:
Derived
Role of the virtual destructor:
During C ++ development, the class destructor used for base classes are generally virtual functions. But why?
Class clxbase
{
Public:
Clxbase (){};
Virtual ~ Clxbase (){};
Virtual void dosomething () {cout <"do something in class clxbase! "<Endl ;};
};
Class clxderived: Public clxbase
{
Public:
Clxderived (){};
~ Clxderived () {cout <"output from the destructor of class clxderived! "<Endl ;};
Void dosomething () {cout <"do something in class clxderived! "<Endl ;};
};
Code
Clxbase * Ptest = new clxderived;
Ptest-> dosomething ();
Delete Ptest;
The output result is:
Do something in class clxderived!
Output from the destructor of class clxderived!
This is very easy to understand.
However, if you remove the virtual before the clxbase destructor, the output result is as follows:
Do something in class clxderived!
That is to say, the destructor of the class clxderived is not called at all! Under normal circumstances, all the class destructor release the memory resources, and if the Destructor is not called, memory leakage will occur. I think all c ++ programmers know this danger. Of course, if you do other work in the destructor, all your efforts will be in vain.
Therefore, the answer to the question at the beginning of the article is -- This is to call the destructor of a derived class when a base class pointer is used to delete an object of A derived class.
Of course, it is not necessary to write all class destructor as virtual functions. When there is a virtual function in the class, the compiler will add a virtual function table to the class to store the virtual function pointer, which will increase the storage space of the class. Therefore, only when a class is used as the base class can the Destructor be written as a virtual function.
Virtual and pure virtual functions
Virtual void foo1 (); // virtual function
Virtual void foo2 () = 0; // pure virtual function
To meet the needs of overload and polymorphism, the virtual function can be rewritten in the subclass or not written to this function in the base class!
Pure virtual functions are not defined in the base class and must be implemented in the subclass, much like interface functions in Java!
Reason for introduction
1. To facilitate the use of polymorphism, we often need to define virtual functions in the base class. (This is the same as a virtual function)
2. In many cases, it is unreasonable for the base class to generate objects. For example, an animal can be derived from sub-classes such as tigers and peacocks as a base class, but the object generated by the animal itself is obviously unreasonable.
Classes with pure virtual functions are called virtual base classes, which are also called abstract classes. This base class cannot directly generate objects. It can only be used after it is inherited and its virtual function is rewritten.