Adding User Defined Class Methods

Web Site

Str Library allows you to add custom (user-defined) methods to the Str class without subclassing, using templates or modifying the source files of the library itself.

To utilize this feature, you must define the symbol STR_OWN_METHODS in your configuration file. This will cause str.h to include an additional file during compilation, whose name is fixed at str_own_method.h

You can place additional class methods in this file. Remember, the file itself is included within the body of Str class declaration.

Sample str_own_method.h file:

// Private str methods
   inline void CapitalizeFirstLetter() {
      if (GetLength() > 0) {
         Char ch (GetAt(0));
         SetAt(0, ch.MakeUpper());
      }
   }
// End of private str methods

If you prefer to have methods implemented in a separate cpp file and not inline in the header, you can just declare the method and then place the implementation in a source file of your choosing (of course, this file must be compiled and linked as part of your project).

A benefit of using this method versus modifying library source code directly is that you can update your projects with newer Str Library editions, either manually or by running the Wizard, and your added methods will not be overwritten.

 

Important: You can add public, private and protected class methods, as well as static class members.  However, you must never attempt to define non-static members, since the class implementation relies on the fact that the class body is always exactly the size of a void* on any platform.

 

See also: Str conditional defines