Don't write inlines in declarations

C++ has two ways of declaring an inline member function. One is to declare the member function normally and then supply an inline function definition later in the same header file. The other is to write the function definition directly in the class declaration. Don't use this latter form--always declare the function normally and then put an inline definition at the end of the file. That way, it's much easier to change between inline and regular implementations of a function, and it's no less efficient. The fact that something is inline should not be obvious in the class declaration because clients might start counting on it.

      class TFoo {
      public:
          int TweedleDee() { return 1; };     // Bad!
          int TweedleDum();                   // Good!
      };
      
      inline int TFoo::TweedleDum()
      {
          return 2;
      };
One exception to this guideline concerns functions that are declared to be empty (usually in abstract base classes); you can write these directly in the declaration, as in:

      class TFoo {
      public:
          virtual ~TFoo() {};                 // Declared to be empty
      };
However, don't do this unless you know you will never, ever change it.


[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker