Summary of C ++ constructor and destructor

Source: Internet
Author: User

1. Constructor

 


Copy constructor ::

Complex (const Complex
& C)
{
// Copy the data member value in Object c
M_real = c. m_real;
M_img = c. m_img;
}

Usage ::

Complex ();

Complex B =;

Operator overload

Complex
& Amp; operator = (const Complex
& Rhs)
{
// First, check whether the object on the right of the equal sign is the object on the left. If the object is itself, the system returns
If (this =
& Rhs)
{
Return * this;
}

// Copy the member on the right of the equal sign to the object on the left.
This-> m_real
= Rhs. m_real;
This-> m_imag
= Rhs. m_imag;

// Output the object on the left of the equal sign again
// The purpose is to support connections.
Eg: a = B = c the system first runs B = c
// Then run a =
(The return value of B = c. Here it should be the B object after copying the c value)
Return * this;
}

Usage ::

Complex a, B

B =;

 


The copy constructor and operator overload are both deep copies instead of light copies. The actual results are the same except for their usage.

Copy ::

As mentioned above, if there is no custom replication constructor, the system will create the default replication constructor, but the default replication constructor created by the system will only execute "shortest copy ", data Member of the object to be copied
Values are assigned to the newly created object. If the data member of this class has pointer members, the pointer of the new object is directed to the same address as the pointer of the Copied object. When you delete the pointer, duplicate delete operations may occur. The following is an example:
 
[Shallow copy and deep copy]
 
# Include <iostream. h>
# Include <string. h>
Class Person
{
Public:

// Constructor
Person (char *
PN)
{
Cout <"a constructor is called.
! \ N ";
M_pName = new char [strlen (pN)
+ 1];
// Open a memory block in the heap to store the string indicated by pN
If (m_pName
! = NULL)
{
// If m_pName is not a null pointer, copy the string referred to by the parameter pN to it.
Strcpy (m_pName, pN );
}
}

// The default copy constructor created by the system. Only the bitwise copy function is used.
Person (Person & p)
{
// Point the two string pointers to the same address location
M_pName = p. m_pName;
}

~ Person ()
{
Delete m_pName;
}

Private:

Char *
M_pName;
};

Void main ()
{
Person man ("lujun ");
Person woman (man );

// Result causes man and woman
Pointer to the same address

// Function end destructor
// Delete the same address twice
}


// The copy constructor is designed below to implement "Deep copy", that is, the pointer is not directed to the same address, but a memory is re-applied to the pointer data members of the new object.
Person (Person & chs );
{
// Use the new operator to allocate space for the pointer data members of the new object.
M_pName = new char [strlen (p. m_pName) +
1];

If (m_pName)
{
// Copy content
Strcpy (m_pName, chs. m_pName );
}

// The m_pName of the newly created object and the m_pName of the original object chs no longer point to the same address
}

2. destructor

We know that the Destructor used for base classes during C ++ development are generally virtual functions. But why? Here is a small example:


There are two classes:

Class ClxBase
{
Public:
ClxBase (){};
Virtual ~ ClxBase (){};

Virtual void DoSomething () {cout <"Do something in class ClxBase! "<Endl ;};
};

Class ClxDerived: public ClxBase
{
Public:
ClxDerived (){};
~ ClxDerived () {cout <"Output from the destructor of class ClxDerived! "<Endl ;};

Void DoSomething () {cout <"Do something in class ClxDerived! "<Endl ;};
};
Code

ClxBase * pTest = new ClxDerived;
PTest-> DoSomething ();
Delete pTest;
The output result is:

Do something in class ClxDerived!
Output from the destructor of class ClxDerived!
This is very easy to understand.
However, if you remove the virtual before the ClxBase destructor, the output result is as follows:

Do something in class ClxDerived!
That is to say, the destructor of the class ClxDerived is not called at all! Under normal circumstances, all the class destructor release the memory resources, and if the Destructor is not called, memory leakage will occur. I think all C ++ programmers know this danger. Of course, if you do other work in the destructor, all your efforts will be in vain.
Therefore, the answer to the question at the beginning of the article is -- This is to call the destructor of a derived class when a base class pointer is used to delete an object of A derived class.
Of course, it is not necessary to write all class destructor as virtual functions. When there is a virtual function in the class, the compiler will add a virtual function table to the class to store the virtual function pointer, which will increase the storage space of the class. Therefore, only when a class is used as the base class can the Destructor be written 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.