More Java Style Classes in C++

I’ll refer you to this explanation for the benefits of Java style classes in C++. The jist of it is that the dividing line between interface and implementation ought to be done with abstract base classes and inheritance rather than header and cpp files. It’s how a good chunk of the STL works, and it’s how a good chunk of Boost works. To me, it eases compilation woes since you no longer have to worry about static versus dynamic linking, and whether your library was built with debug symbols on. Don’t get me wrong, there is still a place for compiled libraries, just a smaller one than there was before.

Anyway, and perhaps this is just me being rusty with my knowledge of the C++ precompiler, but I find the author’s reasoning sharp yet his implementation rusty. Hiding everything inside a global class seems like it’s just asking for trouble, as I recall having some trouble with inner templated classes acting different than normal global classes. I instead prefer this solution – at the top of every header file you have, BEFORE your precompiler firewall, you ‘export’ the names of the types you’re defining, similar to what was always in .h files. Turn this:

#ifndef MYHEADER_H #define MYHEADER_H class foo {}; #endif

into this:

class foo; #ifndef MYHEADER_H #define MYHEADER_H class foo {}; #endif

That way, each time you include a header, only the implementation itself is behind the precompiler firewall, the name is exported fine and can be used. The same problems with circular types need to be resolved, namely, with one type holding the other by pointer or reference, but this isn’t anything new. I haven’t used this technique in practice, so I throw it out to the community as a potential piece of not very well thought out shit. It may only work for a few trophy cases. But it seems to solve the same problems the author is having, and seems a more elegant solution than a global class with #includes inside of it.