"High quality Programming Guide C + + language" Forest Rui Han Yongquan Summary 3

Source: Internet
Author: User

The 9th Chapter class's constructors, destructors and assignment functions

1. Usage rules for constructor initialization tables:

1) If the class has an inheritance relationship, the derived class must call the constructor of the base class in its initialization table.

2) A const constant of a class can only be initialized in the initialization table, because it cannot be initialized in the function body in the same way as an assignment.

3) The initialization of a data member of a class can be assigned two ways in the initialization table or function body, and the efficiency of these two methods is not exactly the same.

2. Constructor and destructor for class String

Ordinary constructor for String

string::string (const char *STR)

{

if (str==null)

{

m_data = new Char[1];

*m_data = ' \ 0 ';

}

Else

{

int length = strlen (str);

m_data = new Char[length+1];

strcpy (m_data, str);

}

}

destructor for String

String::~string (void)

{

delete [] m_data;

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

}

3. Copy constructor and assignment function for class String

Copy constructor

string::string (const String &other)

{

Allow operation of other private members 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-assigned value

if (this = = &other)

return *this;

(2) Releasing the original memory resources

delete [] m_data;

(3) Allocate new memory resources and copy 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;

}

Chapter 10th inheritance and combination of classes

1. Inheritance

Rule 1. If class A and class B are irrelevant, it is not possible to allow B to function more

Inherits the functionality and properties of a.

Rule 2. If logically B is "a" (a kind of) of a, then B is allowed to inherit the functions and properties of a.

2. Combination

The rules. If a is logically a part of B, it does not allow B to derive from a, but to combine B with a and other things.

Suggestions:

Recommendation 1. Beware of those visually indistinguishable operators that write incorrectly.

We often mistakenly write "=" as "=", such as "| |", "&&", "<=", ">=" such symbols are also prone to "lost 1" error. However, compilers do not necessarily automatically indicate such errors.

recommendation 2. Variables (pointers, arrays) should be initialized immediately after they are created to prevent variables that are not initialized are used as rvalue values.

Recommendation 3. Beware of variable initial values, default errors, or insufficient precision.

recommendation 4. Beware of errors in data type conversions. Try to use explicit data type conversions (let people know what's going on), and avoid letting the compiler quietly make implicit data-type conversions.

Recommendation 5. Beware of variable overflow or underflow, array subscript out of bounds.

Recommendation 6. Be careful about forgetting to write error handlers, and be aware that the error handler itself is wrong.

Recommendation 7. Beware of file I/O errors.

Recommendation 8 Avoid writing highly skilled code.

Recommendation 9. Do not design a data structure that is exhaustive and flexible.

Recommendation 10. If the original code quality is better, reuse it as much as possible. But don't fix bad code, you should rewrite it.

Recommendation 11. Use standard library functions as much as possible, and do not "invent" library functions that already exist.

Recommendation 12. Try not to use variables that are closely related to a specific hardware or software environment.

recommendation 13. Set the compiler's selection to the strictest state.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"High quality Programming Guide C + + language" Forest Rui Han Yongquan Summary 3

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.