Composition

Like inheritance, object composition raises similar performance concerns with regard to object creation and destruction. When an object is created (destroyed), all its contained member objects must be created (destroyed) as well. For instance, the Trace object discussed in the previous chapter contains a member object of class string.

class Trace
{
    ...
    string theFunctionName;
};

When you construct an object of type Trace, the constructor invokes the string constructor to initialize the string data member. This behavior is recursive: If class A contains a member of class B and B contains a C, the constructor for A will invoke the construction of a B object which, in turn, will trigger that of a C object. Since at each level in the ...

Get Efficient C++ Performance Programming Techniques now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.