0

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

h.s.
  • 139
  • 9
  • Suppose templates and inheritance are forbidden. Not sure if there is any other scenario where one could justify this. – 463035818_is_not_an_ai Apr 27 '21 at 13:46
  • How does `DEFINE_CLASS` influence the included header? – dyp Apr 27 '21 at 13:47
  • 4
    This looks like the invention of a clever novice, perhaps a mathematician. It's not good enough to be called a pattern, and it's not common enough to be called an anti-pattern. If you're looking for a name for it, *"Frankenstein"* leaps to mind. – Beta Apr 27 '21 at 13:49
  • @dyp, the included file contains data members like `DEFINE_CLASS* pNext;`. – h.s. Apr 27 '21 at 15:28
  • How old is this code? Does it predate templates? It sounds like an intrusive list, and those (like all other container types) have been produced using macros before templates made it into C++. – dyp Apr 27 '21 at 15:34
  • actually it looks like a so called X marco. Basic idea is that the user defines a symbol, include a macro that is "parametrized" on that symbol, and undefines it again. I have seen this in enum to string conversions. And here is a SO question about them:https://stackoverflow.com/questions/6635851/real-world-use-of-x-macros – 463035818_is_not_an_ai Apr 28 '21 at 17:20
  • here the Q&A about enum to string. Sorry, i didnt bother to look for the answers but I remember that there were some that use Xmacros https://stackoverflow.com/questions/28828957/enum-to-string-in-modern-c11-c14-c17-and-future-c20 – 463035818_is_not_an_ai Apr 28 '21 at 17:21
  • @dyp, the code comes from pre-2008. It may date back to the times when templates were not available. – h.s. Apr 30 '21 at 07:30
  • @largest_prime_is_463035818, "X macro" suits. "Frankenstein" was not too bad, I had almost decided to call it "nightmare" :-) Thanks to all contributors! – h.s. Apr 30 '21 at 07:51

0 Answers0