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

Source: Internet
Author: User
Tags constructor

constructors, destructors, and assignment functions are the most basic functions of each class. They are so common that they are easy to slumber, but these seemingly simple functions are as dangerous as sewers without top cover. Each class has only one destructor and one assignment function, but it can have multiple constructors (including one copy constructor, others called normal constructors). For any class A, if you do not want to write the above function, the C + + compiler will automatically generate four default functions for a, such as

A (void); Default parameterless constructor

A (const a &a); Default copy constructor

~a (void); Default destructor

A & operate = (const a &a); The default assignment function

This makes people wonder, since the function can be automatically generated, why do programmers write?

The reasons are as follows:

(1) If you use the default parameterless constructor and the default destructor, you give up the opportunity to "initialize" and "purge", and the C + + inventor Stroustrup kindness in vain.

(2) "The default copy constructor" and "Default Assignment function" are implemented in the form of "bit copy" rather than "value copy", and if the class contains pointer variables, the two functions are doomed to error.

For those C + + programmers who haven't suffered enough, if he says it's easy to write constructors, destructors, and assignment functions, you can use your brain to show that his knowledge is superficial and that the level needs to be improved.

This chapter takes the design and the realization of the class string as an example, and elaborates the truth which is neglected by many textbooks. The string is structured as follows:

Class String

{

Public

String (const char *STR = NULL); Normal constructors

String (const string &other); Copy Constructors

~ String (void); destructor

String & operate = (const string &other); Assignment function

Private

Char *m_data; Used to save strings

};

9.1 Constructors and the origins of destructors
As a more advanced language than C, C + + provides a better mechanism to enhance the security of your programs. C + + compiler has a strict type of security check function, it can almost find out all the grammar problems in the program, this really helps the programmer's great favor. But the program passed the compilation check does not mean that the error no longer exists, in the "wrong" family, "grammatical error" status can only be regarded as a penis. High-level errors are often hidden deep, like cunning criminals, and it is not easy to catch him.

According to experience, many bugs are difficult to detect because the variables are not properly initialized or eliminated, and initialization and cleanup work can easily be forgotten. Stroustrup in the design of C + + language to fully consider this problem and solve it well: the initialization of the object of the work in the constructor, the cleanup work in the destructor. When an object is created, the constructor is executed automatically. When the object dies, the destructor is automatically executed. This does not worry about forgetting the object's initialization and cleanup work.

Constructors and destructors cannot be named arbitrarily, and must be recognized by the compiler to be executed automatically. Stroustrup's naming method is both simple and reasonable: let constructors, destructors, and classes have the same name, and because the purpose of the destructor is the opposite of the constructor, prefix ' ~ ' to differentiate it.

In addition to the name, another special point of constructors and destructors is that there is no return value type, which is different from a function that returns a value type of void. The mission of constructors and destructors is very clear, like birth and death, naked to bare. If they have a return value type, then the compiler will be overwhelmed. To prevent complications, simply stipulate that there is no return value type. (The above allusions refer to the literature [Eekel, p55-p56])

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.