Assignment operator Overloading: An existing object is used to assign a value to another object that already exists and initializes (called the constructor).
Copy constructor: In fact, it is essentially a constructor that constructs an object that does not exist with an existing object.
String A ("Hello");
String B ("World");
String C =a; Copy constructor
c = b; Call Assignment function
In general, when a data member contains a pointer object, one of the two different processing requirements is to copy the pointer object, one that refers to the pointer object
Copy in most cases, = is a reference to the object's
Example:
Class A
{
int Nlen;
char * PDATA;
}
Obviously
A, B;
When A=b, there are two requirements for pdata data
Copy of the first
A.pdata = new char [Nlen];
memcpy (A.pdata, B.pdata, Nlen);
Another way (citation):
A.pdata = B.pdata
First, copy constructor
Class Cexample
{
Public
Cexample () {pbuffer=null; nsize=0;}
~cexample () {delete pbuffer;}
void Init (int n) {pbuffer=new char[n]; nsize=n;}
Private
Char *pbuffer; The object in the class contains pointers to dynamically allocated memory resources
int nSize;
};
int main (int argc, char* argv[])
{
Cexample Theobjone;
THEOBJONE.INIT40);
Now another object needs to be initialized to the state of the object one.
Cexample Theobjtwo=theobjone;
...
}
Statement "Cexample Theobjtwo=theobjone;" Initialize the theobjtwo with Theobjone.
It is done in a memory copy and copies the values of all the members.
When finished, Theobjtwo.pbuffer==theobjone.pbuffer.
That is, they will point to the same place, although the pointer is copied, but the space pointed to is not copied, but is shared by two objects. This does not meet the requirements, the object is not independent, and for the deletion of space to bring hidden trouble.
After the copy constructor is provided, the Cexample class is defined as:
Class Cexample
{
Public
Cexample () {pbuffer=null; nsize=0;}
~cexample () {delete pbuffer;}
Cexample (const cexample&); Copy constructor
void Init (int n) {pbuffer=new char[n]; nsize=n;}
Private
Char *pbuffer; The object in the class contains pointers to dynamically allocated memory resources
int nSize;
};
Definition of cexample::cexample (const cexample& rightsides)//copy constructor
{
Nsize=rightsides.nsize; Copy Regular members
Pbuffer=new Char[nsize]; Copy what the pointer points to
memcpy (pbuffer,rightsides.pbuffer,nsize*sizeof (char));
}
Shallow copy and deep copy
In a simple sentence: shallow copy, just a copy of the pointer, copy the two pointers to the same memory space; a deep copy not only copies the pointer,
and copy the content that the pointer points to, and the deep copy pointer is a pointer to two different addresses.
What is the problem with a shallow copy (bit copy)?
Problem: 1:b.m_data The original memory is not released, causing a memory leak
2:b.m_data and A.m_data point to the same piece of memory, either a or B changes will affect the other party, the object is not independent
3: Object is deconstructed, M_data is destroyed two times.
Example Analysis:
Back Part reprint: Http://blog.csdn.net/feitianxuxue
Shallow copy: That is, when the object is copied, only the data members in the object are simply assigned, if there is a dynamic member in the object, that is, the pointer, the shallow copy will be problematic
#include <stdio.h>
Class A
{
Public
A ()//constructor, p points to a space allocated in the heap
{
m_data = new char (100);
printf ("default constructor \ n");
}
~a ()//destructor, releasing the dynamically allocated space
{
if (m_data! = NULL)
{
Delete m_data;
m_data = NULL;
printf ("destructor \ n");
}
}
Private
Char *m_data; A pointer member
};
int main ()
{
A;
A B (a); A problem occurred while copying an object
return 0;
}
deep copy : For deep copies, the presence of pointers to member variables is not just a simple pointer assignment, but a reallocation of memory space
#include <stdio.h>
#include <string>
Class A
{
Public
A ()//constructor, p points to a space allocated in the heap
{
M_pdata = new char (100);
printf ("default constructor \ n");
}
A (const a& R)
{
M_pdata = new char (100); Re-allocating space dynamically for new objects
memcpy (M_pdata, R.m_pdata, strlen (R.m_pdata));
printf ("copy constructor \ n");
}
~a ()//destructor, releasing the dynamically allocated space
{
if (m_pdata! = NULL)
{
Delete m_pdata;
printf ("destructor \ n");
}
}
Private
Char *m_pdata; A pointer member
};
int main ()
{
A;
A B (a); Copy object, not only assignment of data member, memory space redistribution
return 0;
}
Assignment operator overloading and copy constructors and shallow copy with deep copy