A brief explanation of dynamic and Static Association of C + + and _c language of virtual destructor function

Source: Internet
Author: User

C + + static correlation and dynamic Association, C + + is how to achieve polymorphism
In real life, there are many examples of polymorphism. Let's analyze how people deal with polymorphism. For example, when a freshman is enrolled in a university, when the person learns to report, there is a staff review of the material, his duty is to identify eligibility, and then according to the acceptance of the admission of the Department and the profession, the transfer of materials to the relevant departments and professional, to deal with specific registration procedures, or to call the different departments of the processing procedures for admission procedures. In the eyes of the students, the staff member is the total population, all the freshmen have to go through his entrance procedures. Students take a unified admission notice, but in fact belong to different departments, to carry out different registration procedures, this is polymorphism. So, how does this worker handle polymorphism? Why should I distribute it to which department? It is based on a message from the admission notice (you are admitted to a certain professional in this university). Visible, to distinguish between must have the relevant information, otherwise it is unable to discriminate.

Similarly, the compilation system should judge the invocation of a function of the same name based on the information already available. For example, the overload of a function, the system is based on the number of parameters and types of different to find the matching function. For calling virtual functions in a family of the same class, you should tell the system in a certain way when you call, which is the function of the class object you are calling. For example, you can provide an object name directly, such as Studl.display () or Grad1.display (). This compiles the system to determine which function in the class object is invoked when compiling the program.

The process of determining which specific object is invoked is called an association (binding). Binding intent is to bind or connect two things together. Here is a function name with a class object bundled together to establish the association. Generally speaking, an association refers to associating an identifier with a storage address. In a computer dictionary, the so-called Association refers to the process of connecting different parts of a computer program to each other. In some books, binding is translated into a joint, a compilation, a Banding, or a combination of sounds and meanings, which is called binding. The author thinks: from the meaning, the relevance is more accurate, good understanding. But some tutorials use the terminology of the binder. When you see this noun, you should know that it refers to the association described in this section.

By the way, most of the terms in the computer field are translated from foreign languages, and there are many translated translations that are better and can be seen in the name of the word. But there are some that are puzzling, not even precise. For example, in some books on computer language, project translation into "engineering" makes it difficult to understand, in fact, the translation of "project" is more accurate. Some of the books introduced computer applications flooded with a lot of terminology, at first sounded like a bluff, difficult to understand, many people learning C + + is often a large number of special terminology scare, and difficult to understand its true meaning, many people "see difficult and retreat." This problem has become a stumbling block for many people to learn C + +. Therefore, it should be advocated to articulate complex concepts in a user-friendly way. In fact, there are many seemingly esoteric concepts and terminology, it is very easy to pierce the window paper. It is suggested that readers should not dwell on the literal interpretation of noun terminology while they are beginners, but should grasp its spiritual essence and application methods.

Description: Compared with other programming languages, such as Java, C # and so on, C + + syntax is the richest and most flexible, but also the most difficult to master, we have to step, not to find a quick, in the programming practice of constantly browsing and memory.

The function overload mentioned earlier and the virtual function invoked by the object name, at compile time, determine which class the virtual function that it calls belongs to, which is called a static association (static binding) and is called an early association (early binding) because it is associated before running. A function overload is a static association.

When a virtual function is invoked without specifying an object name, how does the system determine the association? The reader can see that polymorphism is realized through the combination of the base class pointer and the virtual function. A pointer variable that points to the base class is defined, and it points to the corresponding class object, which then invokes the virtual function (for example, "Pt->display ()") through the base class pointer. Obviously, for such a call, the compilation system cannot determine the virtual function of which class object is invoked when compiling the row. Because the compiler only makes static grammar checks, light from the statement form (e.g. "pt->display ();" is unable to determine the calling object.

In this case, the compiling system puts it into the run-time process and determines the association relationship at run time. At run time, the base class pointer variable points to a class object and then calls the function in that object through this pointer variable. The function of which object is called at this point is undoubtedly certain. For example, let PT point to Grad1, then execute "pt->display ()", of course, call the display function in Grad1. Because virtual functions and class objects are "bound" together at run time, this process is known as dynamic binding. This polymorphism is dynamic polymorphism, that is, the polymorphism of the run-time phase.

In the running stage, the pointer can point to different class objects, and then call the virtual functions of the same family. Because dynamic associations are performed at a later stage of compilation, they are also called hysteresis associations (late binding).

A detailed explanation of C + + virtual destructor function
When an object of a derived class is revoked from memory, the destructor of the derived class is called first, and then the destructor of the base class is called. However, if you create a temporary object with the new operator, the Jocky class has a destructor and a pointer variable that points to the base class. When the program uses the delete operator with the pointer parameter to undo the object, a situation occurs when the system executes only the destructor of the base class and does not perform the destructor of the derived class.

[Example] the execution of a non-virtual destructor in the base class. To simplify the program, only the most necessary parts are listed.

#include <iostream>
using namespace std;
Class Point//define base class point class
: Point
  () {}//point constructor
  ~point () {cout<< "executing point destructor "<<ENDL;} Point class destructor
Class Circle:public point//Definition derived class Circle class
  Circle () {}//circle constructor
  ~circle () {cout< < "executing Circle destructor" <<ENDL;} Circle class destructor
  int radius;
int main ()
  *p=new Circle;//Open dynamic storage space with new
  delete p;//release dynamic storage space with delete return

This is just a schematic procedure. P is a pointer variable pointing to the base class, pointing to the dynamic storage space opened by new, hoping to use Detele to release the space pointed to by P. But the results of the operation are:

Executing point destructor

Represents a destructor that executes only the base point, and does not perform a destructor of the derived class circle.

If you want to be able to perform a destructor of a derived class circle, you can declare the destructor of the base class as a virtual destructor, such as:

  Virtual ~point () {cout<<″executing point Destructor″<<endl;}

Other parts of the program do not change, and then run the program, the result is:

Executing Circle destructor
executing point destructor

The destructor of the derived class is called, and the destructor of the base class is called, which accords with the people's desire.

When the destructor of a base class is a virtual function, regardless of which class object is in the same family, the system uses dynamic association, calls the corresponding destructor, and cleans the object.

If you declare a destructor of a base class as a virtual function, the destructor of all derived classes that derive from the base class automatically becomes a virtual function, even if the destructor of the derived class differs from the destructor name of the base class.

It is best to declare a destructor of a base class as a virtual function. This will automatically make the destructor of all derived classes a virtual function. Thus, if a program explicitly uses the delete operator to prepare to delete an object, and the action object of the delete operator uses a base class pointer to a derived class object, the system invokes the destructor of the corresponding class.

The concept and usage of virtual destructor is simple, but it is a very important technique in object-oriented programming.

Professionals are generally accustomed to declaring virtual destructors, even if the base class does not require destructors, and explicitly defines a virtual destructor that is empty for a function body to ensure that it is handled correctly when the dynamic allocation space is revoked.

Constructors cannot be declared as virtual functions. This is because the class object did not complete the build process when the constructor was executed, and certainly not the binding of the function to the class object.

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.