5.2. Copied for Caller
The access model taken by the standard library containers is to give out copies to the caller. Changing our previous Container example to this model would look like Listing 5.3.
Listing 5.3.
class EnvironmentVariable
{
// Accessors
public:
String GetName()const
{
return m_name;
}
String GetValuePart(size_t index) const
{
return index < m_valueParts.size() ? m_valueParts[index] : String();
}
// Members
private:
String m_name;
Vector<String> m_valueParts;
};
This model has some appeal: it's simple to understand and it doesn't have the concerns with respect to the relative lifetimes of the container and the client code. However, returning anything other than fundamental types or simple value types can involve a nontrivial amount of code, with a consequent performance cost.
 |