FAQ 28.01 What are container classes and what are the most common mistakes made with container classes?

graphics/new_icon.gif

Containers are objects that hold other objects (see FAQ 2.15). For example, a linked list of string objects is a container users can add numerous elements (or element objects) to the list, and in this case all the elements are of type string. The closest that the core C++ language comes to container classes is the array but see FAQ 28.02 to find out why C++ arrays should not be used.

Unfortunately programmers' instincts tend to lead them in the wrong direction with containers. This may be because of past experience with other programming languages or it may be for some other reason, but whatever the reason the result is the same: programmers tend to make common and serious mistakes with containers that lead to bugs. Here are a few common mistakes.

  • Using arrays rather than safe container classes (the "Grandma used arrays" fiasco; see FAQ 28.02)

  • Rolling your own container classes from scratch (the "not invented here" fiasco; see FAQ 28.03)

  • Containers of pointers (the "random object ownership" fiasco; see FAQ 28.04)

  • Containers of char* (the "string contents vs. string address" fiasco; see FAQ 28.06)

  • Containers of auto_ptr<T> (the "transfer of object ownership" fiasco; see FAQ 28.07)

  • Inheriting everything from Object (the "based object" fiasco; see FAQ 28.08)

  • Selecting incompatible vendors for container classes (the "best of breed" fiasco; see FAQ 28.10)



C++ FAQs
C Programming FAQs: Frequently Asked Questions
ISBN: 0201845199
EAN: 2147483647
Year: 2005
Pages: 566
Authors: Steve Summit

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net