Terms 28: Smart (SMART) pointer (below)
Translator Note: Since I can't put pictures in the document area (in the forum asking, the result is no one answer), so only the Word documentation of this translation. download
This technique gives us almost all the behaviors that you want. Suppose we use a new class Cassingle to expand the MusicProduct class hierarchy, used to represent Cassette Singles. The modified class hierarchy looks like this:
Now consider this code:
Template
Class SmartPtr {...}; // Member Template for Conversion Operator
Void Displayandplay (Const Smartptr "MusicProduct> & PMP,
Inthnmany;
Void DisplayandPlay (Const Smartptr "Cassette> & PC,
Inthnmany;
Smartptr
DisplayandPlay (Dumbmusic, 1); // Error!
In this example, DisplayandPlay is overloaded, a function with the SmartPtr parameter, other functions of the parameters of other functions, we expect to call SmartPtr
There are two disadvantages of the type conversion of the intelligence pointer through the member template. First, support member templates have few compilers, so this technology does not have portability. There will be change in the future, but no one knows when this will wait. Second, this method is not very clear. To understand it must first understand the parameter matching of function calls, implicit type conversion functions, template functions implicit instantiation and member function template. Some of the programmers have never seen this skill, but they are required to maintain the code of this technique, I am very pity. This skill is indeed very clever, which is naturally affirmation, but it is too dangerous thing to be a dangerous thing.
Don't turn around, say it directly, we want to know that how can we make the dexterous pointer to be the same as the DUMB pointer in the inheritance class. The answer is simple: impossible. As Daniel EDELSON said, the smart pointer is in a smart, but not a pointer. The best way is to use the member template to generate type conversion functions, using CASTS in places that generate unisforced results (see Terms 2). This is not a perfect way, but it is also very good. In some cases, except for the second meaning, the cost paid is worthy of compared to the smart pointer.
Lingqi pointer and const
For DUMB pointers, const can either the pointer to what the pointer itself, or both the meaning of both (see Effective C Terms 21): CD GoodCD ("Flood");
Const CD * p; // P is a Non-Const pointer
/ / Point to the Const CD object
CD * const p = & goodcd; // p is a const pointer
/ / Point to the Non-Const CD object;
// Because P is const, it
/ / Must be initialized
const cd * const p = & goodcd; //p is a const pointer
// Point to a const cd object
We naturally want to make the smart pointers have the same flexibility. Unfortunately, it can only place const in one place and can only work on the pointer itself, but cannot be targeted to the object:
Const SmartPtr
& goodcd; // Point to Non-Const CD objects
It seems that there is a simple remedy, which is to build a smart pointer to the COSNT CD:
SmartPtr
& goodcd; // Point to Const CD objects
Now we can build four different combinations of Const and Non-Const objects and pointers:
SmartPtr
// Non-const pointer
SmartPtr
// Non-const pointer
Const SmartPtr
// Const pointer
Const SmartPtr
// Const pointer
However, it is not enough to use the DUMB pointer to initialize the const pointer with the non-const pointer, and we can also initialize the pointer to the Non-COSNT object; just like assignment. E.g:
CD * PCD = New CD ("Famous movie themes");
Const CD * pconstcd = pcd; // correct
But what if we try to put this method in a smart pointer?
Smartptr
SmartPtr
SmartPtr
These rules look like the rules inherited from public inheritance (Effective C Terms 35). You can convert a derived class object into a base class object, but it is not the case, you can do anything to the base class, but you can do some things to do. We can use this to practice the smart pointer, that is, let each of the smart pointers PUBLIC derived from a corresponding Const-T of the intersection pointer:
Template
Class SmartPTRTOCONST {/ 灵巧 pointer
... // Spiritic pointer usually
// member function
protected:
Union {
Const t * constpointee; // Let SmartPTRTOCONST access
T * pointee; // Let SmartPtr access
}
}
Template
Class Smartptr: // Lingering Pointer
Public SmartPTRTOCONST
... // No data member
}
Using this design method, pointing to the Non-Const-T object contains a DUMB pointer to the Const-T, pointing to the integrity pointer to Const-T requires a DUMB pointer to COSNT-T. The most convenient way is to put the DUMB pointer to the const-t in the base class, put the DUMB pointer to the non-const-t in the derived class, but do some waste, because the SmartPtr object contains two DUMB pointers: One is inherited from SmartPTRTOCONST, one is smartptr himself.
A vintage weapon in the Class C can solve this problem, which is Union, which is equally useful in C . Union in protected, so two classes can access it, which contains two must-have DUMB pointer types, SmartPTRTOCONST
Smartptr
SmartPTRTOCONST
Evaluation
The discussion of the dexterity pointer is over. Before we leave this topic, you should ask such a question: the smart pointer is so cumbersome, whether it is worthwhile, especially if your compiler lacks support member function template.
It is often worth it. For example, the reference count code in Terms 29 is greatly simplified by using the dexterity pointer. Moreover, as shown in this example, the use of smart pointers is extremely limited in some areas, such as test null values, converted to DUMB pointers, and inherits the support to base classes, and supports pointers to Const. At the same time, the practical, understanding, and maintenance of the dexterity pointer requires a lot of skill. Debug uses the code of the smart pointer than the code of the DUMB pointer using the DEBUG. No matter how you are not possible to design a generic purpose, you can replace DUMB pointers.
The same code effect is achieved, which is more convenient to use the dexterity pointer. The delegation should be used with caution
,
Only
C
The programmer will eventually find them useful.