"Effective C + +" study notes-clause 07

Source: Internet
Author: User

*************************************** Reprint Please specify the Source: Http://blog.csdn.net/lttree ********************************************


Ii. Constructors,destructors and Assignment Operators


Rule 07:declare destructors Virtual in polymorphic base classes

Rule 07: Declare the virtual destructor for the polymorphic base class



1. A simple example to trigger a "murder"

A table's class:

Class Timekeeper  {public:    timekeeper ();    ~timekeeper ();    ...};

It is then used as a base class to derive atomic clocks, water clocks (drips), and watches.

Class Atomicclock:public timekeeper  {...};    Atomic Clock class Waterclock:public timekeeper  {...};    Hourglasses class Wristwatch:public timekeeper  {...};    Watches

This is a very reasonable, standard example, but many people just want to use the time in the program and not care how it calculates the details (this is the class), so we can design a factory (factory) function, and the return pointer points to a timed object. The factory function returns a base-class pointer to the newly generated derived object.

timekeeper* PTK = Gettimekeeper (); <span style= "White-space:pre" ></span>//returns a pointer timekeeper the dynamically allocated object of the derived class

To obey the rules of the factory function, the object returned by Gettimerkeeper () must be in the heap. Therefore, in order to avoid leaking memory and other resources, it is important to delete each object returned by the factory function as appropriate:

timekeeper* PTK = Gettimekeeper ();.. delete PTK;

All of these things are normal and necessary, but one thing that follows from the 13th clause is that depending on the client performing the delete action, there is basically some kind of error tendency. The 18th article, though, talks about how the factory function interface can be modified to prevent common customer errors.

But what is done in this article is to deal with the fundamental weaknesses of the above code: (according to the book) even if the client has done everything right, there is still no way to know how the program will act.


2. Enter the case

where is the problem? --The pointer returned in the above gettimekeeper is an object that points to a derived class, and that object is deleted by a base class pointer, and there is a non-virtual destructor in the current base class.

Why is there a problem here? (reason)--c++ explicitly states that when an object of a derived class is deleted by a base class pointer, and the base class takes a non-virtual destructor, the result is undefined (in short, the part that is derived is not destroyed).

In the popular point, the base class part is destroyed, but the derived class part is not destroyed, the phenomenon of partial destruction will lead to the waste of resources.

out of the question, of course to solve --the solution to this method is also very simple, is to give the base class A virtual function.

Class Timekeeper  {public:    timekeeper ();    Virtual ~timekeeper ();    ...}; timekeeper* PTK = Gettimekeeper ();.. delete PTK;

This solves the problem, which also embodies the role of virtual-allowing the implementation of derived classes to be customized.

By the same note--☆ Any class with the virtual function is almost certain that there should also be a virtual destructor ☆

This extends to the idea that if a class does not have a virtual function, it means that the class wants to be a base class, and that if a class doesn't want to be a base class but still has a virtual function for it, that's not a good practice.


However, even if there is no virtual function within a class, there are many examples of trips that have been tripped by the Non-virtual destructor.

Like what:

Class Specialstring:public std::string  {    ...};

This class inherits the string class, but it is a non-virtual destructor inside the string class, so it is tripped over. ~

Therefore, be aware of all classes without virtual destructors, such as all STL containers (vector, list, set, and so on)

(Java and C # do not have to worry about this because Java has a final classes and C # has sealed classes this "prohibit derivation" mechanism )


3. Are there any other related cases?

To know it--pure virtual--pure virtual function

In this rule, the pure virtual destructor is also mentioned for this problem, which results in the abstract class, which is a class that cannot be manifested.

This is very nice for the following situations:

--you want a class to be purely a base class, but there is no pure virtual function that can be declared abstract class (it can also be simply declared as a class, not declaring an object on it, but what if the hand is broken and the object is declared?) Always leave a safety hazard)

At this point, you can declare a pure virtual destructor!

Just like the following:

Class Awov  {public:    virtual ~awov () = 0;};

In this way, it has pure virtual functions, so it is an abstract class, cannot define an entity, and because it is a virtual destructor, so do not worry about the destructor mentioned above problem.

Then add a definition for this destructor:

Awov::~awov ()  {  }

Well, here in a sentence, the function of the destructor, similar to the stack, advanced out, the deepest derived classes, first, and then sub-destructor, and finally the base class of the destruction of the execution.


4. The amount ... The virtual destructor of the base class?

Yes, the base class can also have virtual destructors, isn't that a conflict with the above? Of course, this is also conditional, to the base class virtual destructor, this rule only applies to the polymorphic (polymorphic) base class, the above timekeeper is the Polymorphic property of the base class.


5.Please Rmember

The ★polymorphic (polymorphic) base class should declare a virtual destructor. If class has any virtual function, it should have a virtual destructor.

★classes is not intended to be declared as a virtual destructor if it is not used as a base class, or is not intended to be polymorphic.




*************************************** Reprint Please specify the Source: Http://blog.csdn.net/lttree ********************************************

"Effective C + +" study notes-clause 07

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.