Guide to High quality C++/C Programming-9th Chapter-Class constructors, destructors, and assignment functions (3)

Source: Internet
Author: User
Tags constructor strlen

9.3 Sequence of construction and deconstruction
Constructs begin at the root of the class hierarchy, and in each layer, the constructor of the base class is called first, and then the constructor of the member object is invoked. Destructors are executed strictly in the opposite order of construction, which is unique, otherwise the compiler will not be able to automate the destructor.

An interesting phenomenon is that the order of member object initialization is completely unaffected by the order in which they are initialized in the initialization table, and is determined only by the order in which the member objects are declared in the class. This is because the declaration of the class is unique, and the constructor of the class can have multiple, so there are several initialization tables in different order. If the member objects are constructed in the order of the initialization table, this will cause the destructor to not get a unique inverse. [Eckel, p260-261]

9.4 Example: Constructors and destructors for class string
Common constructors for string

string::string (const char *STR)

{

if (str==null)

{

m_data = new Char[1];

*m_data = ' n ';

}

Else

{

int length = strlen (str);

m_data = new Char[length+1];

strcpy (m_data, str);

}

}

The destructor of string

String::~string (void)

{

delete [] m_data;

Since m_data is an internal data type, it can also be written as a delete m_data;

}

9.5 Do not despise copy constructors and assignment functions
Because not all objects use copy constructors and assignment functions, programmers may have some contempt for these two functions. Please remember the following warning before you read the text:

You have said at the beginning of this chapter that if you do not actively write copy constructors and assignment functions, the compiler will automatically generate default functions in a "bit copy" manner. If the class contains pointer variables, then these two default functions implicitly have an error. Take the two object of class string, a,b, for example, assuming that A.m_data's content is "Hello" and B.m_data's content is "world".

Now assigns A to B, and the "bit copy" of the default assignment function means the execution B.m_data = A.m_data. This will result in three errors: First, B.m_data original memory has not been released, resulting in memory leaks; the second is that B.m_data and A.m_data point to the same memory, a or b any one side of the change will affect the other side; third, when the object is destructor, the M_data is released two times.

The U copy constructor and assignment function are very confusing, often resulting in incorrect writing and error-prone. A copy constructor is invoked when an object is created, and an assignment function can only be invoked by an object that already exists. In the following procedure, the third statement is very similar to the fourth statement, and you can see which one called the copy constructor, and which one called the assignment function?

String A ("Hello");

String B ("World");

String C = A; The copy constructor is called, preferably written as C (a);

c = b; An assignment function was called

In this example, the third statement is less styled and should be rewritten as String C (a) to distinguish it from the fourth statement.

9.6 Example: Copy constructors and assignment functions for class string
Copy Constructors

string::string (const String &other)

{

Allow private members to manipulate other m_data

int length = strlen (Other.m_data);

m_data = new Char[length+1];

strcpy (M_data, other.m_data);

}

Assignment function

String & string::operate = (const string &other)

{

(1) Check self assignment

if (this = = &other)

return *this;

(2) Releasing the original memory resources

delete [] m_data;

(3) Allocating new memory resources and copying content

int length = strlen (Other.m_data);

m_data = new Char[length+1];

strcpy (M_data, other.m_data);

(4) Returns a reference to this object

return *this;

}

The difference between a class string copy constructor and a normal constructor (see section 9.4) is that NULL is not required to be compared at the entrance of a function because references cannot be null and pointers can be null.

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.