Some problems of initialization and assignment
FANCY on January 5, 2005
The magic of C is to have a class that you are in mind, but there is still such defects and errors. First, let us clearly identify the difference between initialization and assignment. For C , a new object is created, there is an initialization operation; assignment is a value that modifies the object that has been created, this time does not have a new object being created. ESTRING P = Q; // Initialization, the new object P is created p = q; // // assignment, the value of the existing object P is changed
Initialization is implemented by the constructor and assigns the value by the Operator = operator.
There are two easily confused concepts in the constructor: "Default constructor" and "default replication constructor". The former does not need any parameters to be called constructor; the latter is a replication constructor that is automatically generated by the compiler. If we don't declare a replication constructor for classes, then the compiler will automatically generate one.
When we write a class, we often encounter the behavior of the default replication constructor and our expectations.
Please see the following programs, a string class. Class estring {private: char * pstr public: estring (const char * str = ""; virtual ~ estring (); void release ()}; estring :: estring (const char * str) {pstr = new char [strlen (STR) 1]; STRCPY (PSTR, STR);} estring :: ~ estring () {if (! pstr) delete [] PSTR;} void estring :: release ()} void Estring ::
In the above ESTRING class, the default replication constructor of the ESTRING class copies the PSTR pointer. If I do the following: Estring P ("Hello"); estring q = p; p.Release (); then the PSTR in the object Q will point to the released memory, because P and Q in PSTR actual Both points to the same memory area. This is a very serious problem, so we must improve this type of design to ensure that every object has a separate data copy.
So we need yourself to declare a copy constructor: class estring {private: char * pstr public: estring (const char * str = "); virtual ~ estring (); estring (const estring & s); void release ()} Estring :: estring (const char * str) {pstr = new char [strlen (str) 1]; struct (pstr, str);} estring :: ~ estring () {if (! Pstr) delete [] PSTR } Estring :: estring (const Estring & s) {pstr = new char [strlen (s.pstr) 1]; strcpy (pstr, s.pstr);} void estring :: release () {delete [] PSTR; } To achieve your own copy constructor, each object can have a copy of the data.
After solving the problem of default replication, we will find out that the default assignment operator will exist.
We do the following code: Estring P ("Hello"); Estring q; q = p; p.release (); previously default replication constructor problems, and appearing in assignment operators. Also we need yourself to overload the assignment operator. Const Estring & Estring :: Operator = (const Estring & s) {if (pstr! = s.pstr) {delete [] pstr; pstr = new char [strlen (s.pstr) 1]; strcpy (PSTR, S.pstr } Return * this;
After the overloaded assignment operator, you can avoid the pointer to the same memory area, so that each object has its own independent data.
In general, we need to pay more attention when treating those with pointers, and for them, the default replication constructor and the default assignment operator may be inappropriate, they simply copy the pointer. Not those important data pointed to those pointing. This is some of the problems recently, I hope to give some little help.