One of the constructor: The constructor itself is a lot of content time, and the understanding is first. The back is slowly written and discussed. Here, this mainly tells the COPY structure, and some issues initialized in the sequence. I hope everyone will pay attention.
2004-12-2 20:00
<1> Shallow copy text Problem: Bitwise Copy "Effective C " uses this noun to describe, only to see the original version is still understandable, translated, I use a shallow copy. Simple definition I think it is: Simple replication of all member objects.
Through example, let's take example.
Class A
{
PUBLIC:
A (const char * ip_c);
~ A ();
Private:
Char * mp_c;
}
A :: A (const char * ip_c)
{
IF (ip_c)
{
MP_C = new char [strlen (ip_c) 1];
STRCPY (MP_C, IP_C);
}
Else
MP_C = New char ('/ 0');
}
A :: ~ a ()
{
IF (MP_C)
DELETE [] MP_C;
MP_C = NULL;
}
Int main (int Argc, char * argv [])
{
A t_a_1 = "test1"; // This is not a problem constructor to construct a object and A :: mp_c open space
A t_a_2 = "test2"; // Nothing to construct a object A :: mp_c, another open space
T_A_1 = T_A_2;
// This place is because there is no copy construct or Operate =
// Only the copy constructor that calls the default compiler to add a member copy
// We only grasp the focus, if it is an object to copy everything, because the object copy
// The need to reassign the space does not have a shared space. But pay attention to this member is a pointer
/ / This time the pointer copy is only four bytes of content copying equivalent to the pointer assignment, and no
/ / Reesence new T_A_1.mp_C release space and then copy the contents of T_A_2.mp_c
Return 0;
}
// Generate a problem 1: Due to the simplicity of T_A_1.mp_c = t_a_2.mp_c, the address of the original T_A_1.mp_c is lost, the memory is not // method release
// Generate Problem 2: Due to the simple assignment, the memory pointer of the two objects points to the same piece address. At the end, the object sector // delete []
// cause an error twice.
// Potential problem: In any function call to pass a value for A, you will have a problem 2 wrong.
// Recommendation 1: If you have a pointer member constructor, please write a custom Copy Costruct, and complete copies. Avoid with //
// Recommendation 2: If you want to use, use it when you use it. Avoid generating a temporary object sector release multiple times.
<2> Pseudo-consoic initialization sequence. This initialization sequence will have some small problems that will happen from time to time when not paying attention. It makes people feel confused. I often encounter. So this question is given to make it don't realize what you have to understand, don't make the same mistake like even.
#include
#include
Using namespace std;
Class C
{
PUBLIC:
C () {}
C (C &) {COUT << "C: << endl;}
}
Class D
{
PUBLIC:
D () {}
D (D &) {COUT << "D: << endl;
}
Class A
{
PUBLIC:
A (C & I_C, D & I_D): m_c (i_c), m_d (i_d) {}
/ / Generally does not contain a class template construction sequence from left to right // can execute everyone can perform it, but if the template will be prioritized to perform the template construction a (int i_i, const char * i_P_c): m_i (i_i), m_s (i_p_c) {}
// Here you first perform M_S constructs and then in turn M_i
/ / Because there is a sequence of sequence because there is a special order of the template
// Be especially careful. M_s's constity must be serialized
// Time structure.
A (int i_i): m_i (i_i), m_v (m_i) {}
// Here constructor priority M_V, if m_i is not static, will be wrong
// This also implies a implicit conversion problem below
Private:
Vector
INT M_I;
Const string m_s;
C m_c;
D m_d;
}
void main ()
{
INT T_I = 5;
Const char * t_p_c = "const";
C t_c;
D t_d;
A t_a_t (t_c, t_d);
A t_a_c (t_i, t_p_c);
// a t_a_s (t_i); // This time is constructed because of the priority construction M_T, the m_i has not been constructed yet.
// The Vector is wrong when allocator.
}
Recommended:
1. Const member can only initialize when serialization, do not attempt to process within the constructor. Unless you spoof
2. Sequence construction sequence, so you should pay attention to the order when you use, do not build objects above unconstructive objects
3. Template is particularly specialty in serialization, use incoming parameters when using the incoming parameter, try not to use members.
If it is not static. You can also have better handle in the constructor.