mardi 27 avril 2021

Weird C++ design pattern: Class inheritance via include files

I came across a C++ coding pattern that is so weird that I'd really like to know where it comes from. Some classes (base classes, no ancestors) need to share certain functionality. Normally you would implement this as an ancestor class (a template if need be) from which those classes inherit which need the functionality.

Here, the programmer chose to pack the required method and data member declarations into a separate file and include this file in midst of the class declaration. They even #define'd a symbol to the name of the class being defined before the #include line. This created declarations like this:

class CMyStructure
{
public:
  CMyStructure();
  ~CMyStructure();

  __int64 m_SomeData;
  double m_SomeOtherData;

  #define DEFINE_CLASS CMyStructure
  #include "include_definitions.h"
  #undef DEFINE_CLASS
};

The included code made use of the DEFINE_CLASS symbol.

The same pattern is repeated in the implementation file, where a different file is included which defines some methods.

Does this pattern have a name? Where does it come from?

(A warning to C++ newbies: Don't use this pattern!)

Thanks, Hans

Aucun commentaire:

Enregistrer un commentaire