5.3. Given to Caller
A simple model, where applicable, is to give the instances to the caller. It isn't widely useful, but it does find appeal in some circumstances. An example would be when implementing a container that is seldom traversed more than once, and whose elements occupy a large amount of memory and/or other resources. Maintaining copies in these circumstances is likely to be a wasted effort, so they can just be given to the caller. This model is therefore more efficient than the vouched-lifetimes model (see Listing 5.4).
Listing 5.4.
class EnvironmentVariable
{
// Accessors
public:
. . .
String const *GetValuePart(size_t index) const
{
String *item;
if(index < m_cParts)
{
item = m_store.Load(index);
}
else
{
item = 0;
}
return item;
}
// Members
private:
int m_cParts;
DiskStore m_store;
};
|