C + + Object-oriented Advanced development course (second week)

Source: Internet
Author: User

1. The case with pointers--class with pointer member (s) in a class often occurs, typically: the String class.

2. The string class in STL is too complex, copy on write, and so on.

3. Use "Defensive header file declaration".

4. S2 assignment to S3.

String S3 ("hello"= s2;

5. Complier is generated by default: Copy constructors and copy assignment functions (operator overloading), which are executed in a bitwise order. a class with pointers is not suitable for using the default constructor.

// Copy Construction // Copy Assignment

6. The implementation principle of the String class

classstring{ Public: String (Const Char* cstr=0); Private:    Char* M_DATA;//The pointer dynamically allocates storage space}string::string (Const Char*CStr) {   if(CStr) {m_data=New Char[Strlen (CStr) +1];   strcpy (M_data, CStr); }   Else{m_data=New Char[1]; *m_data =' /'; }}

C + + object-oriented Advanced development course (second week)

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.