A recent study of the Python reference manual has learned the class section and encountered problems with the structural destructor of the class:
1. When is the construction?2. When is the destructor?3, the member variable how to deal with?4. How do shared member functions
PHP object-oriented programming advanced features (interface, inheritance, abstract class, destructor, clone, etc.), c abstract class destructor
This article describes the advanced features of PHP object-oriented programming. We will share this with you for your reference. The details are as follows:
Static attributes
All C ++ programmers are familiar with destructor, so they can be applied quickly due to their simplicity and ease of understanding. I will not talk about these common methods here. If you do not know, refer to C ++ books. What I want to talk about this time is a more subtle technique that is often not noticed, but is very important. See the following code:
//\\////\\////\\////\\////\\////\\//\\// //\\////\\//
// Example 1
// Author: Yuan Kai
// Date:
, and finally the Destructor _ del __. Constructor and destructor are not explicitly called here and are automatically executed.
Class destructor
In python, the form of class destructor
){++ Numtanks ;}
~ Enemytank () {-- numtanks ;}
Static size_t numberoftanks (){Return numtanks ;}
Virtual bool destroy ();
PRIVATE:Static size_t numtanks; // tank object counter};
Finally, assume that an enemytank object is dynamically created using new somewhere else in the program, and then deleted using delete:
Enemytarget * targetptr = new enemytank;
...
Delete targetptr
This will cause a serious problem, because the C ++ language standards are very clear about thi
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 parent pointer, if you do not define a virtual destructor, only the
C + + clearly states: when the derived class object is deleted by a base class pointer, and the base class takes a non-virtual destructor, the result is undefined-- The actual execution usually occurs when the derived component of the object is not destroyed! (Note: Using polymorphism in the way that a base
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 it is necessary to establish a timekeeper base class (base class), and
Constructors and Destructorsconstructor Function:Used to initialize the content part state of the class, Python provides a constructor-style __init__ (); the __init__ () method is optional, and if not provided, Python gives the default __init__ method general data acquisition needs to define the get and set methodsDestructors :Used to release the resource occupie
creating a derived class object, the sequence of calling constructor is as follows: base Class Construction --> sub-Object Construction --> derived class construction. The Calling sequence of the Destructor is strictly opposite to that of the constructor. In the constructor of a derived
Polymorphism is realized by virtual function table, and dynamic binding is realized by parent class pointer. When a subclass overrides a virtual function of the parent class, it overrides the virtual function in the table of the virtual function in its original position in the table, which may be called "overwrite".
Is that the same as the destructor? The
Call of constructor and destructor in class inheritanceCurrently, there are three classes. The class definition is as follows:Class ca{Public:CA () {cout ~ CA () {cout };Class CB: public CA{Public:CB () {cout ~ CB () {cout };Class CC: Public CB{Public:CC () {cout ~ CC () {co
Main content:1. Constructors and destructors in C + + class inheritance2. Static binding and dynamic binding in C + + polymorphism3. The destructor is declared as virtual function in C + + polymorphism1. Constructors and destructors in C + + class inheritanceIn the class inheritance of C + +,When an object is created,
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, a
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 methods for timing. Because there are many timing methods, it is worthwhile to do so:
Class constructor, destructor, and value assignment function
Constructors, destructor, and assignment functions are the most basic functions of each class. They are so common that they are easy to paralyze. In fact, these seemingly simple functions are as dangerous as the sewers without the top cover.
Each
Clause 07 declares virtual destructor for the polymorphism base class, And the polymorphism virtualDeclare virtual destructor for the polymorphism base class
In the inheritance for Polymorphism purposes, if the virtual destructor is not declared for the base
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: The base class pointer can point to the object (polymorphism) of the der
During polymorphism, it is best to set the destructor of the base class to virtual. In this way, the destructor of the subclass will be called first, and then the destructor of the base class will be called, otherwise, if Delete is a pointer to the base
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.