1. Virtual function
Cause: When you call a member function through a pointer, you can only access the member function with the same name as the base class. In a overwrite with the same name, a function with the same name is called by the object (pointer and reference) of a class, and the compiler statically compiles the call to a function of that class with the same name, that is, a function with the same name cannot be accessed through a base class object , even if the pointer is initialized with a derived class object.
Virtual functions are the mechanisms used in C + + to implement polymorphism (polymorphism). The core idea is to access the functions defined by the derived class through the base class .
When a pointer to a base class operates its Polymorphic class object, it calls its corresponding function according to different class objects, which is a virtual function.
It can be said that the virtual function declared by the base class is also a virtual function in the derived class, even if the virtual keyword is no longer used.
dynamic linking: the invocation of a class function is not determined at compile time, but is determined at run time. It is a "virtual" function because it is not possible to determine the function of the base class or the derived class when the code is written.
A virtual function can only be used with pointers or references to achieve polymorphic effects.
A *p=new B () meaning: A refers to the parent class, A * p is a pointer to a parent class p, and a child class B. The P pointer itself is a parent class type, and also points to a. A tree and an apple tree Appletree. Since the pointer p is pointing to the apple tree, it must also point to the tree.
First: Emphasize a concept
Defines a function as a virtual function and does not represent a function that is not implemented.
Defines him as a virtual function to allow a pointer to a base class to invoke this function of a subclass.
Defines a function as pure virtual function, which means that the function is not implemented.
A pure virtual function is defined to implement an interface that acts as a specification, and a programmer who inherits this class must implement this
2, "Overload" and "override"
Override means that the derived class overrides the virtual function of the base class, just as we have overridden the Foo () function in Class A in the Class B above. The overridden function must have a consistent parameter table and return value (the C + + standard allows
The return value is different, this I will briefly describe in the "syntax" section, but few compilers support this feature). This word seems to have no suitable Chinese words to correspond to, some people translated into
"Overwrite", also appropriate some.
The Overload convention idiomatic is translated as "overloaded." is to write a function with the same name as an existing function but with a different parameter table. For example, a function can accept an integer as an argument, or it can accept a floating-point number as an argument.
3. Pure virtual function
The following declaration indicates that a function is a pure virtual function:
Class A
{
Public
virtual void foo () = 0; =0 flag A virtual function is a pure virtual function
};
When a function is declared as pure virtual, the meaning of pure virtual function is: I am an abstract class! Don't instantiate me! pure virtual functions are used to standardize the behavior of derived classes, which are actually called "interfaces". It tells the user that my derived class will have this function.
The introduction of pure virtual functions is for two purposes:
1, for the sake of security, because to avoid any need to clear but because of carelessness caused by the unknown results, remind the subclass to do the implementation should be done.
2, for efficiency, not the efficiency of program execution, but for the efficiency of coding.
4. Virtual destructor function
There are only virtual destructors, no fictional functions.
When you create a derived class object, call the base class construct, derived class constructs, and the derived class destructor-and-base class destructor.
If the derived class object is dynamically created with the new operator and the base class pointer is initialized with this object address, the construction is fine, but when you delete the derived class object with the delete operator, because the pointer is pointing to the base class, the base class destructor is called by the static binder, and the derived class destructor is not called. Makes it impossible for derived classes to perform some cleanup work, for example: the memory requested in a derived class has no chance of being returned to the system.
Virtual Destructors: Base classes set virtual destructors, and derived classes are. When you use a base class object pointer to destroy a derived class object, the derived class destructor is called through the dynamic binder to complete the cleanup of the derived class.
5. Use
When you design a base class, if you find that a function needs to behave differently in a derived class, it should be virtual . From a design standpoint, a virtual function that appears in a base class is an interface, and a virtual function that appears in a derived class is the concrete implementation of the interface. In this way, you can abstract the behavior of an object.
C + + (ix)-virtual function