Item 7: declare Destructors (destructor) as virtual in polymorphic base classes (polymorphism base class)
By Scott Meyers
Translator: fatalerror99 (itepub's nirvana)
Release: http://blog.csdn.net/fatalerror99/
There are many ways to get the time, so
The theoretical premise of virtual destructor isAfter executing the destructor of the subclass, the fictitious function of the parent class is bound to be executed.Then when you use Delete to release a subclass object that is instantiated by a
Polymorphism is a basic attribute of object-oriented, including static polymorphism (compile phase) and dynamic polymorphism (runtime phase). Static polymorphism mainly refers to the polymorphism of different function parameters, which is a
From the syntax above, it is possible for destructors to throw exceptions, and C + + does not prevent destructors from throwing exceptions, but C + + does not recommend this practice, and it is dangerous to throw exceptions from
C ++ inherits the calling sequence and virtual destructor of the destructor in the destructor.
First, let's talk about the constructor. We all know that the constructor can call member variables, and the Child class in inheritance is to turn the
Document directory
The constructor cannot be declared as a virtual function. The Destructor can be declared as a virtual function, and sometimes it must be declared as a virtual function.
The constructor cannot be declared as a virtual function.
In C ++, virtual constructor cannot be declared, but virtual destructor can be declared. Polymorphism means that different objects have different behavior characteristics for the same message. As the basis of Runtime polymorphism, virtual functions
Reference: http://www.weixueyuan.net/view/6373.htmlSummarize:A constructor cannot be declared as a virtual function, and a destructor can be declared as a virtual function.After the destructor of the base class is declared as a virtual function, the
In the previous article, we introduced why we should avoid writing copy constructors and value assignment operators. Today we will discuss why we should avoid writing code in the destructor. That is, leave the Destructor empty.
For example:
virtual
turn from: http://blog.csdn.net/liuning800203/article/details/6455226
Releasing resources with destructors
Destructors are used to deconstruct instances of classes.
1 The destructor cannot be defined in the structure. Destructors can only be used
Declare the destructor of the polymorphism base class as a virtual function.
Now we want to consider a timer problem. First we create a base class named timekeeper, and then create various derived classes based on it, so that we can use different
Chapter Review:The 1th chapter of effective C + + makes himself accustomed to c++-reading notesEffective C + + 2nd structure/destructor/assignment operation (1)-Reading notes"Effective C + +" 8th custom new and delete-reading notesArticle 05:
Destructor usage:1. Each destructor (without virtual) is only responsible for clearing its own members. 2. There may be a base class pointer pointing to the case where it is indeed a member of a derived class.For the second case:Correct execution:
C + + destructor
When an object is created, the system automatically calls the constructor to initialize the work, as well as the system automatically calls a function to clean up when the object is destroyed (for example, the various resources
C + + static correlation and dynamic Association, C + + is how to achieve polymorphismIn 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,
C + + destructor
The inherited development environment that I use: Visual Studio
How to write a destructor when designing a class.Destructors if we do not write,C + + will help us to automatically synthesize one, that is to say:C + + will
Constructor:
C ++ provides constructor to process object initialization. It is automatically executed when an object is created. The name of the constructor must be the same as the class name. It does not have any type or return any value. The
1. destructor and virtual destructorIf 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
Constructor and destructor manage the creation and release of objects respectively, and take charge of the process of object birth and death. When an object is born, the constructor is responsible for creating and initializing the internal
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.