1. If a class is going to be a base class, it should have a virtual destructor to prevent the "partial destruction" problem that occurs when the delete is directed to a derived class object by a base-class pointer, avoiding memory leaks, corrupted data structures, wasting ti
The definition of the class and the instantiation of the objectThe members of the //class are class hero{ public $name that belong to the object;//member Variable // Member properties (in strongly typed languages) protected $blood; private $attack; //member methods function skill () { echo "$this->name } } $s = New Hero ();//Create a hero $s->n
important to note that the standard library typically requires a type that is stored in a container to have an assignment operator, and its return value is a reference to the left operand.Synthetic copy assignment operatorAs with the copy constructor, if a class does not define its own copy assignment operator, the compiler generates a synthetic copy assignment operator for him. Like a copy constructor, for some classes, the synthetic copy constructi
A destructor is a function that is called automatically when the life cycle of an object ends.In the following function, execution will find that only the destructor of the base class is called, and the destructor of the derived class is not called, causing the object not to
#!/usr/bin/env python#-*-coding:utf-8-*-# File: Destruction method del.py# author:huxianyong# date:2018/7/9class Foo: # Initialize Party # # # # # # # # # # # # # # # # # # # # # The object is automatically called Def __init__ (Self, name): Print (' __init__ method called ') Self.name = name # destructor # when the object is When deleted, is automatically called and then frees Memory Def __del__ (self): pri
Let's take notes first:The content of this class is about
constructor function
Destructors
Operator overloading
return * This
The content is very fine, everybody reviews the note to take care of the procedure review:)#include using namespacestd;classInteger { Public: inti; intGeti ()Const{return This-i;} voidSETI (inti) { This->i =i;} Integer (intj =0); Integer (Integerc); ~Integer ();};i Nteger::integer (Integer c) {//Co
A destructor is primarily used to release resources, such as freeing a database connection or a picture resource, or destroying an object.
1. Destructors are automatically invoked
2. The main method of destructor is to release resources
3, the destructor call order is: the first object created, and then destroyed.
4. When is the
# Include If the Destructor without the base class is not declared as virtual, The destructor of the subclass will not be executed.// Declare the destructor of the parent class as a virtual function. The function is to use the pointer of the parent
To sum upFor exampleC1 * P = new C2 ();Delete P;Such code
Here, C1 is the base class of C2. C1 may be the father of C2, grandpa, or Grandpa of C2, it may be grandpa's grandfather ..............................
First, the called constructor isFrom the first ancestor of C2 to C2 ................... It's okay with C1.
When deleting P, there are the following situations:1) if the ancestor (base class) of C1
allocation in the constructor, you should also use Delete in the destructor to release the memory through new; however, if an exception occurs in the constructor (after the new statement), the constructor will exit if there is no exception handler. Because the object is not fully constructed, the object's destructor will not be called, that is, the delete statement is not executed, causing memory leakage;*
Learning materials• base class and derived class destructor execution orderDefining derived class destructorsNote : When you define an object, you call the constructor of the base class, and then call the constructor of the derived class
class Timekeeper { Public: Timekeeper (); ~timekeeper (); Error, which is inherited as a base class. The base class is destroyed after its inherited class is deleted, but the inheriting class may not be destroyed virtual ~timekeeper (); //must be declare
Test the source code:Test the call order of the constructors of derived classes when calling//fedora20 gcc version=4.8.2#include Test code:int main () {A a; b b;}The output is:watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvcxe4ndqzntixntu=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">You can see that when you create an object of a derived class, you call the constructor in the base
In any Linux system, in the GCC 4.1.1 or 4.1.2 environment, create the following project:
Makefile
Test: Test. O Records. oG ++-O test. O Records. o
Role. O: role. cpp role. h base. hG ++-g-C-wall-O branch. O branch. cpp
Test. O: Test. cpp protocol. h base. hG ++-g-C-wall-O test. O test. cpp
Test. cpp
# Include "example. H"
Int main (INT, char **){Caggretive OBJ;Return 0;}
Base. h
# Include
Class cbase{Public:Cbase () {printf ("cbase/N ");}~ Cbas
[Php] Use php constructor and destructor to compile Mysql DataBase Query Class
In the previous article "[php] uses the original JavaScript Ajax for MVC hierarchical design for php and is compatible with IE6" (click to open the link), I used php to query the Mysql database model. php is not well written. In each method, you need to declare the $ con object of mysql and disable the $ con object of mysql. In t
PHP class and object, constructor and Destructor, plus this binding details
I. classes and objects
Not only in PHP, but in all object-oriented programming languages, classes are abstract and objects are an instance of classes. All abstractions are: "extract similar ones ".
In life, people are similar to each other in terms of facial features, hair, hands and feet, eating, sleeping, and so on.
Let's take
For the class inheritance/initialization of member variables/constructor/destructor execution sequence, see the following code:
# Include
The output of the above Code is as follows:AABAABC~ C~ B~ A~ A~ B~ A~ A
A: When initializing a subclass, initialize the parent class in the same sequence as the inheritance class.
There are two methods to create a class:1. Declare a method name with the same class name in the class (PhP4, not recommended ).2. Declare a _ construct () method.Because the constructor is automatically called after the object is created, the constructor is used to initialize the member attributes of the class (assign
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.