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那就是一个大大的挑战。