C++物件導向進階開發課程(第二周)

來源:互聯網
上載者:User

標籤:

1. 類中含有指標—— class with pointer member(s) ——的情況經常發生,典型的有:string 類。

2. STL中的 string 類太複雜,copy on write 等等特性。

3. 採用“防衛式標頭檔聲明”。

4. s2 賦值給 s3。

String s3("hello"), s2;s3 = s2;

5. complier 會預設產生:拷貝建構函式 和 拷貝賦值函數(操作符重載),其執行的原理是按位依次賦值。帶指標的類不適合使用預設的建構函式。

String s3(s1); //拷貝構造String s2 = s3; //拷貝賦值

6. String 類實現原理

class String{  public:    String(const char* cstr=0);  private:    char* m_data; //指標動態分配儲存空間}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 = ‘\0‘;   }}

 

C++物件導向進階開發課程(第二周)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.