《The C++ Standard Library》第42頁 一段很難理解的話

來源:互聯網
上載者:User

第42頁最下面幾行字:

寫的莫名其妙。

下面一步一步的分析:

1. The copy constructor指的是:

當執行

的時候,auto_ptr的ownership被從ptr1轉移到了ptr2,此時ptr2指向new ClassA所產生的對象,而ptr1就不再指向new ClassA所產生的對象了,而是以null 指標作為它的值了,所以說:

其中這裡的the object that is used to initialize the new object指的就是ptr1.

2. The assignment operator指的是:

在ptr2=ptr1中,auto_ptr的ownership從 ptr1轉移到了ptr2,也就是說,此時ptr2指向new ClassA所產生的對象。

然後,ptr1就不再指向new ClassA所產生的對象了,而是以null 指標作為它的值了。所以說:

這裡的right-hand side of the assignment 就指的是ptr1.

3. 由於被用來進行copy constructor或者assignment的auto_ptr最後都會指向null,所以最後一句說:

意思就是要這樣產生的,指向null的指標不要再被dereference了。

現在另一個問題產生了,怎麼樣會導致一個變成null的auto_ptr被dereference那?

下面是dereference的定義:

to   get   the   content   which   was   pointed   by   a   pointer   of   a   pointer-like   object   (for   instance   a   iterator)

也就是說,不能再對它進行讀取內容之類的操作了。

道理很簡單,因為它已經指向null了,這裡要提醒我們的是不要忘了這種情況下它已經以null 指標作為它的值了,不要再傻乎乎的對它進行操作了,會出大亂子滴

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.