C ++ pure virtual function (reprint)

Reproduced in: https://www.runoob.com/w3cnote/cpp-virtual-functions.html

First, the definition

Pure virtual function is declared in the base class virtual function, it is not defined in the base class, but requires that any derived class must define your own implementation. A method to achieve pure virtual function in the base class is added after the function prototypes  = 0:

virtual void funtion1()=0

 

 

Second, the introduction of reason

1, in order to facilitate the use of multi-state properties, we often need to be defined in the base class virtual function.

2, in many cases, objects are generated base class itself unreasonable. For example, an animal as a base class can be derived tigers, peacock subclass of the animal itself generates a significant anomaly objects.

 

To solve the above problems, the concept of a pure virtual function, the function will be defined as a pure virtual function (Method: Virtual Function ReturnType () = 0;), it requires the compiler must be overridden in a derived class to achieve polymorphism . Class also contains pure virtual function is referred to as an abstract class, it is not generated object. This solves both of these problems.

It declares a pure virtual function of the class is an abstract class. Therefore, the user can not create an instance of the class, it can only create an instance of the derived class.

Pure virtual function most notable features are : they must re-declare the function in a derived class (not the back = 0, otherwise the derived class can not be instantiated), but they are often not defined in the abstract class.

The purpose of defining pure virtual function in that the derived classes inherit just the interface functions.

Meaning pure virtual function, so that all class objects (mainly derived class objects) can perform actions pure virtual function, but the class can not provide a reasonable default implementation for the pure virtual functions. So pure virtual function declaration class is a subclass of the designer told, "You must provide an implementation of pure virtual functions, but I do not know how you will achieve it."

 

Introduce an abstract class

An abstract class is a special class, which is for the purpose of design abstraction and for the establishment of, it is in the upper layers inheritance hierarchy.

(1) defines an abstract class:  said class with a pure virtual function is an abstract class.

Effect (2) abstract classes:  primary role abstract class is related to the operation as a result of the interface tissue in an inheritance hierarchy, to provide a common root for the derived class by its derived class will be specifically implemented in the base class as an operation of the interface. Therefore, the derived class actually depicts a set of actions common semantics interface subclass, the subclass pass these semantics, specific subclasses can implement these semantics, the semantics can then pass their subclasses.

(3) Note that when using an abstract class:

  • An abstract class can be used as a base class that implements pure virtual function is given by the derived classes. If the derived class does not redefine pure virtual function, but only inherited pure virtual function in the base class, the derived class it is still an abstract class . If the derived class is given in the base class pure virtual function, the derived class is not an abstract class, and it is a specific object can create a class.

 

to sum up:

  • 1, pure virtual function declared as follows:  Virtual void funtion1 () = 0;  pure virtual function is not necessarily defined , pure virtual function is used to regulate the behavior of the derived class, i.e. the interface. It contains pure virtual class is an abstract class, an abstract class instance can not be defined, but can declare a pointer pointing to the concrete classes implement the abstract class or reference.

  • 2, the virtual function declaration as follows: Virtual ReturnType FunctionName (the Parameter) virtual function must be implemented, if not achieved, the compiler will report an error, the error message is:

    error LNK****: unresolved external symbol "public: virtual void __thiscall ClassName::virtualFunctionName(void)"
  • 3, for the virtual function, the parent class and subclass has its own version. When called by the multi-state mode dynamic binding.

  • 4, subclass that implements pure virtual functions, the pure virtual function in the subclass to the programming virtual function, i.e., subclass grandchildren subclass can override this class virtual function, when called by the multi-state mode dynamic binding.

  • 5, virtual function in C ++ is the mechanism used to achieve polymorphism (polymorphism) of. The core concept is the base class to access the derived class definition through.

  • 6, when there is dynamically allocated on the heap memory of the virtual destructor function must be, but need not be pure imaginary.

  • 7, the friend is not a member function, it can only be a virtual member functions, so a friend can not be virtual functions. But it can make a virtual friend function call virtual member functions to solve the problem of a friend.

  • 8, the destructor should be virtual function calls the appropriate object type destructor, therefore, if the pointer points to a subclass object, calls the destructor subclasses, and then automatically calls the destructor for the base class.

Pure virtual function class is abstract, the object can not be generated, only derived. He derived class pure virtual function is not rewritten, then it's derived class is an abstract class.

Defines a pure virtual function is to allow the group of the class is not instantiated, such as instances of abstract data structure itself does not make sense, or does not make sense given to achieve.

In fact, I personally think that the introduction of a pure virtual function, is for two purposes:

  • 1, for safety, by avoiding any need to clear the result of an unknown but because of carelessness caused remind subclasses do realize that should be done.
  • 2, for efficiency, the efficiency of the program is not executed, but to the coding efficiency.

Guess you like

Origin www.cnblogs.com/FdWzy/p/12381281.html