Home > Back-end >  Virtual function in c
Virtual function in c

Time:11-02

Just started to see the virtual keyword, for this thing to understand is not too clear, the virtual keyword that I understand is a pointer to a subclass of the base class dad * a=new son (), if the dad class method using the virtual keyword in the definition, the son also defines this method, so the method that subclass overrides the superclass method, if there is no virtual, call the superclass method, do not know right, hope bosses, correct

CodePudding user response:

Virtual is tells the compiler, the position taken in a virtual table placeholder, save a space, the corresponding virtual table function Pointers to 0, for behind the reserved method interface inheritance, polymorphism

CodePudding user response:

Received, ali ga is much,,,

CodePudding user response:

Virtual function is one of the cornerstone of polymorphism, is defined as the parent class, the actual use of time can be introduced into subclasses, and a subclass of virtual rewritten, so in the end use is a subclass of function, so it is more flexible,

CodePudding user response:

reference weixin_37910058 reply: 3/f
virtual function is one of the cornerstones of polymorphism, is defined as the parent class, the actual use of time can be introduced into subclasses, and a subclass of virtual rewritten, so in the end use is a subclass of function, so it is more flexible,

Isn't this virtual only if the base class pointer and subclass interconnected will only take effect when ah, inheritance of MAO

CodePudding user response:

Virtual function is mainly used in class, if there is no virtual, so there is not much to use c + + inheritance mechanism actually, but with virtual is different, the inheritance mechanism to live,

CodePudding user response:

For example, have a class in the game, monster,
Each monster has a chopping motion (which is a function)
A subclass of the object, a small blame; A subclass of the object, small blame b; In addition a subclass object, bossA;
The same chopping action, according to different object, the damage to a player value is different; Here with the help of virtual,
(the above is a shallow)

CodePudding user response:

Example:
 
#include
using namespace std;

The class father
{
Public:
Father ()
{
Cout & lt; <"Father init" & lt; }
Virtual void DOS ()
{
Cout & lt; <"Father do" & lt; }
};
The class son: public father
{
Public:
Son ()
{
Cout & lt; <"Son init" & lt; }
Void DOS ()
{
Cout & lt; <"Son do" & lt; }
};

Int main ()
{
Son s;
Spyware doctor OS ();
return 0;
}

Results:
Father init
Son init
Son do
If you remove virtual
 
#include
using namespace std;

The class father
{
Public:
Father ()
{
Cout & lt; <"Father init" & lt; }
Void DOS ()
{
Cout & lt; <"Father do" & lt; }
};
The class son: public father
{
Public:
Son ()
{
Cout & lt; <"Son init" & lt; }
Void DOS ()
{
Cout & lt; <"Son do" & lt; }
};

Int main ()
{
Son s;
Spyware doctor OS ();
return 0;
}

Results:
Father init
Son init
Father do
Equivalent to a virtual said this function will be subclasses override (polymorphism)
Personal humble opinions and is for reference only!
  • Related