Pointers, References and Values by Mike Crawford Continued...

When to Use Whole-Object Member Variables

Storing members by value improves performance when they are small or frequently allocated.

While I do recommend that you prefer using pointers for member variables rather than storing them by value, you should not always do so. Here are situations when you might choose to store the member as a whole object, that is, to store it by value:

An important case where you cannot store a member by value is when the member is being used polymorphically (especially when the base class contains a pure virtual function - then the class declaration will fail to compile). You will usually want a pointer to the member's base class in this case, at times a reference.

next page previous page contents all programming tips titles

Copyright © 2000, 2001, 2002, 2005 Mike Crawford. All Rights Reserved.

One Must Not Trifle With Wizards For It Makes Us Soggy And Hard To Light