C Language

Weighing the Pluses and Minuses

The previous examples are by no means the only way you can get yourself in trouble with C++'s OOP facilities. In fact, I chose some of the simpler cases to fit within the scope of this tutorial. In Further Reading, I describe several excellent books that delve into the topic more deeply.

The main point I want to emphasize is that C++ objects can either simplify or complicate programming, depending on how thoughtfully their classes are implemented. C++ objects are supposed to make working with complex program structures as straightforward as working with C's built-in types. Keeping that in mind, I recommend that you avoid using C++ classes unless they've been implemented in a way that makes them safe and simple to use. A program is weakened, not strengthened, by using classes that produce surprising results under some conditions.

The other lesson to be learned from examining the complexities of C++ is that great care and lots of experience is necessary before you can implement complex classes that are safe and simple to use. As a result, it's better to enter this realm of C++ programming slowly and cautiously. And, even after developing some mastery over OOP (Object Oriented Programming), it's a good idea to resist the seduction of C++ features, like operator overloading, that look clever, but increase-rather than reduce-program complexity.

Next, following these guidelines can help the pluses of C++ outnumber the minuses.

C Coding Suggestions

  • Use // for comments everywhere but in macro definitions.

  • Use const variables or enumerations, instead of macros, to define mnemonics for constant values.

  • Use in-line functions and templates instead of function-like macros.

  • Define all member functions outside their respective class declaration.

  • Use in-line functions sparingly.

  • Use REF and other macros to define & and other C++ symbols.

  • Use the C++ new and delete operators instead of the malloc() and free() functions.

  • Where possible, use C++ stream I/O instead of the standard C library routines.

  • In a complex expression, use parentheses to explicitly define how the expression is evaluated.

  • Understand the lifetime of object data.

  • Implement assignment functions so that the same object can appear on both sides of the = operator.

  • Avoid overloading operators other than =.