On the destructor of C + + base class for virtual function _c language

Source: Internet
Author: User

1. Reasons:

When implementing polymorphism, when using a base class pointer to manipulate a derived class, prevent only the destructor of the base class without the destructor of the derived class at the time of the destructor.

2. Example:

(1),

#include <iostream>
using namespace std;

Class base{public
:
  Base () {};  ~base () {cout << "Output from the destructor of Class base!" << Endl;  void DoSomething () {cout << ' do something in class base! ' << Endl;
};

Class Derived:public base{public
:
  Derived () {};  ~derived () {cout << "Output from the destructor of Class derived!" << Endl;  void DoSomething () {cout << ' do something in class derived! ' << Endl;
};
 int  Main () { 
 derived* p = new Derived;
P->dosomething ();
Delete p;
return 0;
}        

Run Result:

Do something in class derived!

Output from the destructor of class derived!

Output from the destructor of class base!

The destructor of a base class in code is not a virtual function, and a member of the inheriting class is manipulated with a pointer to an inherited class in the main function, and the process of releasing the pointer P is to release the resource of the inheriting class and then release the base class resource.

(2),

#include <iostream>
using namespace std;

Class base{public
:
  Base () {};  ~base () {cout << "Output from the destructor of Class base!" << Endl;  void DoSomething () {cout << ' do something in class base! ' << Endl;
};

Class Derived:public base{public
:
  Derived () {};  ~derived () {cout << "Output from the destructor of Class derived!" << Endl;  void DoSomething () {cout << ' do something in class derived! ' << Endl;
};
 int  Main () { 
 base* p = new Derived;
P->dosomething ();
Delete p;
return 0;
}        

Run Result:

Do something in class clxbase!
Output from the destructor of class clxbase!

The destructor of a base class in code is also not a virtual function, except that the member of the inheriting class is manipulated with a pointer to the base class in the main function, and the process of releasing the pointer P is to release the resource of the base class, without invoking the destructor of the inheriting class. Call the DoSomething () function to perform a function that is also defined by the base class.

In general, such a deletion can only delete the base class object, but not the subclass object, resulting in the deletion of half image, resulting in memory leaks.

In public inheritance, the operations of a base class on a derived class and its objects can affect only those members inherited from the base class. If you want to manipulate a non-inherited member with a base class, you define this function of the base class as a virtual function. The destructor should naturally be the same: if it wants to deconstruct the redefinition or new members and objects in subclasses, it should be declared virtual as well.

(3),

#include <iostream>
using namespace std;

Class base{public
:
  Base () {};  Virtual ~base () {cout << "Output from the destructor of Class base!" << Endl;  virtual void dosomething () {cout << ' do something in class base! ' << Endl;
};

Class Derived:public base{public
:
  Derived () {};  ~derived () {cout << "Output from the destructor of Class derived!" << Endl;  void DoSomething () {cout << ' do something in class derived! ' << Endl;
};
 int  Main () { 
 base* p = new Derived;
P->dosomething ();
Delete p;
return 0;
}        

Run Result:

Do something in class clxderived!
Output from the destructor of class clxderived!
Output from the destructor of class clxbase!

The destructor of a base class in code is defined as a virtual function, and the member of the inheriting class is manipulated with a pointer to the base class in the main function, and the process of releasing the pointer P is to release the resource of the inheriting class and then call the destructor of the base class. Call the DoSomething () function to perform a function that is also defined by the inheriting class.

3. Summary:

A base class pointer can point to an object (polymorphism) of a derived class. If you delete the pointer delete p, the derived class destructor that the pointer points to is called, and the destructor of the derived class automatically calls the base class's destructor, so that the entire derived class object is completely freed. If the destructor is not declared as a virtual function, the compiler enforces a static binding, and when the base class pointer is deleted, only the base class's destructor is called and the derived class destructor is not invoked, which causes the derived class object to be not completely destructor. Therefore, it is necessary to declare a destructor as a virtual function.

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.