Managing Pointer Members

C++ Primer 4/e在Managing Pointer Members這個地方有一個忠告:『Objects with pointer members often need to define the copy-control members. If we rely on the synthesized versions, then the class puts a burden on its users. Users must ensure that the object to which the member points stays around for at least as long as the object that points to it does.

To manage a class with pointer members, we must define all three copy-control members: the copy constructor, assignment operator, and the destructor. These members can define either pointerlike or valuelike behavior for the pointer member.

Valuelike classes give each object its own copy of the underlying values pointed to by pointer members. The copy constructor allocates a new element and copies the value from the object it is copying. The assignment operator destroys the existing object it holds and copies the value from its right-hand operand into its left-hand operand. The destructor destroys the object.

As an alternative to defining either valuelike behavior or pointerlike behavior some classes are so-called “smart pointers.” These classes share the same underlying value between objects, thus providing pointerlike behavior. But they use copy-control techniques to avoid some of the pitfalls of regular pointers. To implement smart pointer behavior, a class needs to ensure that the underlying object stays around until the last copy goes away. Use counting (Section 13.5.1, p. 495), is a common technique for managing smart pointer classes. Each copy of the same underlying value is given a use count. The copy constructor copies the pointer from the old object into the new one and increments the use count. The assignment operator decrements the use count of the left-hand operand and increments the count of the right-hand operand. If the use count of the left-hand operand goes to zero, the assignment operator must delete the object to which it points. Finally, the assignment operator copies the pointer from the right-hand operand into its left-hand operand. The destructor decrements the use count and deletes the underlying object if the count goes to zero.

These approaches to managing pointers occur so frequently that programmers who use classes with pointer members must be thoroughly familiar with these programming techniques.』

中文版的這樣寫:『帶有pointer成員的物件,常常需要定義copy-control(拷貝控制項)成員。如果倚賴合成版本,這些classes會帶給用戶壓力–用戶必須確保pointer成員所指物件至少和指向底層物件的物件同時存在。

為了管理帶有pointer成員的class,我們必須定義三個copy-control(拷貝控制項)成員:copy建構式,assign運算子和解構式。這些成員可以為pointer成員定義出所謂pointer-like(類似指標)或value-like(類似值)行為。

Valuelike classes為每個物件提供專屬的底層複件給pointer成員去指。copy建構式會配置一個新元素,然後從複製源拷貝出對應值出來。assignment運算子會銷毀目的端的既有物件,再把右運算元(來源端)的值複製到左運算元。解構式則是逕自銷毀物件。

你可以任意選擇要定義value-like行為或pointer-like行為。某些classes被稱為smart pointer(智慧型指標),它們在各物件之間共用同一個底層值,因此提供的是pointer-like行為。但它們使用copy control技術避免一般pointer的缺陷。為了實現出smart pointer的行為,classes必須確保底層物件總是存在,直至class的最後一個複件消失。參用計數(13.5.1節,p.495)是一種管理smart pointers的常用技術。相同底層值的每一個複件都有一個參用計數。其copy建構式把原物件中的pointer複製到新物件,並累加參用計數。其assignment運算子遞減左運算元的參用計數並累加右運算元的參用計數:如果左運算元的參用計數減為0,就必須刪除所有物件。最後assignment運算子再把右運算元的pointer複製到左運算元。其解構式則是遞減參用計數,如果減為0就刪除底層物件。

小心:這些用來管理pointers的方法是如此頻繁地出現,程式員如果使用含pointer成員的class,務必徹底熟悉這些編程技巧。』

pointer是一個挑戰,用class也是一個挑戰,如果在class中用pointer那就是一個大大的挑戰。

Print Friendly, PDF & Email

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料