Pointers, References and Values by Michael D. 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.

Vote for Us at the Programming Pages Voting for GoingWare at The Programming Pages will encourage more people to read these articles.

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

Call GoingWare: +1 (831) 401-3790, info@goingware.com