From the implementation of the string class, we can see four major functions of the C ++ class and four major functions of the string class.

Source: Internet
Author: User

From the implementation of the string class, we can see four major functions of the C ++ class and four major functions of the string class.

I attended an interview a long time ago. I remember the interviewer asked me a very basic code question: implement four basic functions of the string class!

A c ++ class generally has at least four major functions: constructor, copy constructor, destructor, and value assignment function. However, the default system is not what we expect, so we need to create them on our own. Before creation, you must understand their functions and meanings, so that you can write effective functions in a targeted manner.

 

1 # include <iostream> 2 3 class CString 4 {5 friend std: ostream & operator <(std: ostream &, CString &); 6 public: 7 // No-parameter constructor 8 CString (); 9 // constructor with parameters 10 CString (char * pStr ); 11 // copy constructor 12 CString (const CString & sStr); 13 // destructor 14 ~ CString (); 15 // value assignment operator overload 16 CString & operator = (const CString & sStr); 17 18 private: 19 char * m_pContent; 20 21}; 22 23 inline CString:: CString () 24 {25 printf ("NULL \ n"); 26 m_pContent = NULL; 27 m_pContent = new char [1]; 28 m_pContent [0] = '\ 0'; 29} 30 31 inline CString: CString (char * pStr) 32 {33 printf ("use value Contru \ n "); 34 m_pContent = new char [strlen (pStr) + 1]; 35 strcpy (m_pContent, pStr); 36} 37 38 I Nline CString: CString (const CString & sStr) 39 {40 printf ("use copy Contru \ n"); 41 if (sStr. m_pContent = NULL) 42 m_pContent = NULL; 43 else44 {45 m_pContent = new char [strlen (sStr. m_pContent) + 1]; 46 strcpy (m_pContent, sStr. m_pContent); 47} 48} 49 50 inline CString ::~ CString () 51 {52 printf ("use ~ \ N "); 53 if (m_pContent! = NULL) 54 delete [] m_pContent; 55} 56 57 inline CString & CString: operator = (const CString & sStr) 58 {59 printf ("use operator = \ n"); 60 if (this = & sStr) 61 return * this; 62 // order is important, to prevent memory application failure, m_pContent is NULL63 char * pTempStr = new char [strlen (sStr. m_pContent) + 1]; 64 delete [] m_pContent; 65 m_pContent = NULL; 66 m_pContent = pTempStr; 67 strcpy (m_pContent, sStr. m_pContent); 68 return * this; 69} 70 71 Std: ostream & operator <(std: ostream & OS, CString & str) 72 {73 OS <str. m_pContent; 74 return OS; 75} 76 77 78 int main () 79 {80 CString str3; // call the parameter-free constructor 81 CString str = "My CString! "; // Declare a string, equivalent to calling the constructor 82 std: cout <str <std: endl; 83 CString str2 = str; // declare a string, equivalent to calling the constructor 84 std: cout <str2 <std: endl; 85 str2 = str; // call the overloaded value assignment operator 86 std :: cout <str2 <std: endl; 87 return 0; 88}

Output:

NULL
Use value Contru
My CString!
Use copy Contru
My CString!
Use operator =
My CString!
Use ~
Use ~
Use ~

 

 

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.