C ++ multi-state virtual function destructor abstract class

Source: Internet
Author: User

1. destructor and virtual destructor
If the destructor of the base class is virtual, The destructor of its derived class are virtual.
This will cause: When the derived class destructor, all of its basic class destructor will be called.
Otherwise, only the destructor of the derived class is called (this may cause some objects of the base class not to be released)
Therefore, the destructor of the cobject class is virtual. When all the class destructor derived from it are performed at the first level, it will not cause memory leakage.

Whether the destructor of the base class is a virtual destructor or not. the destructor of the base class is always automatically called. However, if a base class pointer is used to operate a derived class object, if it is not virtual, The destructor of the derived class cannot be called.

2. Pure virtual destructor

The unique effect of the pure virtual nature of the Destructor is to ensure the instantiation of the abstract class.

Part of the 14th clause in Objective C ++ is both a thorough understanding of virtual destructor and an explanation of the role of pure virtual destructor.

It is convenient to declare pure virtual destructor in some classes. Pure virtual functions generate abstract classes -- classes that cannot be instantiated (that is, objects of this type cannot be created ). Sometimes you want to make a class an abstract class, but there is no pure virtual function. What should I do? Abstract classes are intended to be used as base classes. The base classes must have a virtual destructor. Pure virtual functions generate abstract classes, so the method is simple: declare a pure virtual destructor in the class to be an abstract class.

Here is an example:
Class awov {
Public:
Virtual ~ Awov () = 0; // declare a pure virtual destructor
};

This class has a pure virtual function, so it is abstract, and it has a virtual destructor, so it will not produce the problem of destructor. But here is another thing: the definition of pure virtual destructor must be provided:

Awov ::~ Awov () {}// definition of pure virtual destructor

This definition is required, because the virtual destructor work in the following way: the destructor of the bottom-layer derived class is called first, and then the destructor of each base class is called. That is to say, even an abstract class, the compiler must generate a pair ~ Make sure to provide the function body for the awov call. If you do not do this, the linker will detect it and you have to add it back.

3. Virtual Functions

[1] declare a member function as a virtual function with virtual in the base class. In this way, you can redefine this function in the derived class, assign it new functions, and be conveniently called.

[2] to redefine this function in a derived class, the function name, function (return) type, number of function parameters, and type must be the same as the virtual function of the base function. If the virtual function of the base class is not redefined in the derived class, the derived class simply inherits the virtual function of the base class.

[3] C ++ stipulates that when a member function is declared as a virtual function, all functions of the same name (in accordance with the functions defined in 2) in the derived class will automatically become virtual functions.
[4] defines a pointer variable pointing to a base class object and points it to an object in the same class family. You can call this function using this pointer variable. In this case, the pointer variable points to an object with the same name.
[5] You can only declare a member function of a class using virtual declaration to make it a virtual function, rather than declaring a common function outside the class as a virtual function.
[6] after a member function is declared as a virtual function, classes in the same class can no longer define a non-virtual function, but have the same parameters (number and type) as the virtual function) a function of the same name as the function return value type.
[7] static member functions cannot be virtual functions, because static member functions are not limited to an object.
[8] the inline function cannot be a virtual function, because the inline function cannot dynamically determine its position during running. Even if a virtual function is defined within a class, it is considered non-inline during compilation.
[5] using virtual functions requires a certain amount of space overhead. When a class has a virtual function, the compiler constructs a virtual function table (vtable) for the class. It is a pointer array that stores the entry address of each virtual function.
4. Pure virtual functions
After a function is declared as pure virtual, the pure virtual function means: I am an abstract class! Don't instantiate me! Pure virtual functions are used to regulate the behavior of derived classes. They are actually called "interfaces ". It tells the user that my Derived classes will all have this function.
Virtual void show () = 0; // pure virtual function

Here, we declare show () as a pure virtual function (pure virtual function ). A pure virtual function is a virtual function that is "initialized" to 0 when a virtual function is declared.
The general form of declaring pure virtual functions is,
Virtual function type function name (parameter list) = 0;

Pure virtual functions do not have function bodies. The final "= 0" does not mean that the return value of the function is 0. It only acts as a formal function and tells the compiler that "this is a pure virtual function "; this statement is followed by a semicolon.
Declaring a pure virtual function tells the compiler that "a virtual function is declared here, which is left for definition in the derived class ". This function can be called only when it is defined in a derived class.
A pure virtual function is used to reserve the name of a function for its derived class in the base class, so that the derived class can define it as needed.
If a pure virtual function is declared in a class but is not defined in its derived class, the function is still a pure virtual function in the derived class.

1. virtual functions and pure virtual functions can be defined in the same class. Classes containing pure virtual functions are called abstract classes, and classes containing only virtual functions are called abstract classes) cannot be called abstract class ).
2. virtual functions can be directly used, or they can be called in the form of polymorphism after being overloaded by subclasses. Pure virtual functions must be called in sub classes) can be used only when this function is implemented, because the pure virtual function is in the base class)
Only declarations are not defined.

3. Both virtual and pure virtual functions can be overloaded in sub class and called in the form of polymorphism.

4. virtual functions and pure virtual functions are usually stored in abstract base class-ABC. The inherited subclass is overloaded to provide a unified interface.

5. virtual function definition form: Virtual {method body}
Definition of pure virtual functions: Virtual {} = 0;
There cannot be a static identifier in the definition of virtual and pure virtual functions. The reason is very simple. The static-modified functions require bind in the early stage during compilation, however, virtual functions are dynamically bound (run-time bind), and the life recycle function modified by the two is different.
6. if a class contains pure virtual functions, any statements that attempt to instantiate the class will produce errors, because the abstract base class (ABC) cannot be directly called. The quilt class must inherit from the overload and then call its subclass method as required.

5 pure abstract classes

From the perspective of C ++, there is no difference between an abstract class and an interface. Sometimes, we use the word "pure abstract class" to indicate that a class only contains pure virtual functions (excluding any data members). It is the most common form of abstract classes.

What are the advantages of pure abstract classes? The most obvious example is "multi-interface, single implementation", which is a very common situation.

Added the concept of pure virtual function to C ++. A pure virtual function must be overwritten by its derived class. With this concept, you can declare a member function as a pure virtual function in a C ++ class to indicate that the class is a pure interface class. Since then, I have always stressed that in C ++, there is a main way to use classes that do not contain any state, but only serve as an interface.

6 abstract classes

Abstract class is a class that can be inherited only as a basic type instead of defining objects. abstract base classes are usually called abstract base classes. All classes that contain pure virtual functions are abstract classes.
If no pure virtual functions are defined in the derived class, the derived class is still an abstract class and cannot be used to define objects.
You can define pointer variables pointing to abstract class data. When the derived class becomes a specific class, you can use this pointer to point to the object of the derived class, and then use this pointer to call the virtual function.

Classes with pure virtual functions are called abstract classes. An abstract class is a special class established for the purpose of abstraction and design. It is at the upper layer of the hierarchy of inheritance. Abstract classes cannot define objects. In practice, to emphasize that a class is an abstract class, you can describe the constructor of this class as a protected access control permission. The main function of an abstract class is to organize the relevant organizations in an inheritance hierarchy to provide them with a public root. The related subclass is derived from this root. Abstract classes depict the General Semantics of the Operation interfaces of a group of child classes. These semantics are also passed to child classes. In general, an abstract class only describes the operation interfaces of this group of sub-classes, and the complete implementation is left to the sub-classes. An abstract class can only be used as a base class. The implementation of its pure virtual function is provided by the derived class. If the derived class does not redefine the pure virtual function, and the derived class only inherits the pure virtual function of the base class, the derived class is still an abstract class. If the implementation of the basic class pure virtual function is provided in the derived class, the derived class is no longer an abstract class. It is a concrete class that can create objects.

7 interfaces in Java

No need to declare the interface as abstract or virtual (originally)
Constructor is not allowed for an interface (it is purely abstract and does not need to be constructed at all)
The interface cannot have destructor (no constructor is required)
All interface members are abstract (pure abstract class)
The interface can only inherit from the interface (because only the interface can ensure that it is purely virtual. If it is inherited from the abstract class, there may not be non-Abstract members in the abstract class)
Interface members are not allowed to have any modifier (the default is public, and only public)
One class or structure can implement multiple interfaces

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.