The Skeptical Methodologist

Software, Rants and Management

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.

March 27, 2009 - Posted by | Uncategorized

2 Comments »

  1. If MYHEADER_H is defined during compilation then the header was already included at an earlier point and class Foo was already defined and compiler knows about it and doesn’t need the predefine. Your change doesn’t do anything except introduce an unnecessary predefine.

    Comment by Alex | April 3, 2009 | Reply

  2. Remember, we’re attempting to use ‘java style classes’ where the implementation is in the .h itself. That means we can’t use the .h for forward declares anymore, as is normal style (leaving implementation for a cpp file).

    Since implementation tends to include way more types than a standard interface-style .h, circular reference becomes a major issue.

    The alternative way to implement this, as the link at the beginning of the article states, is via a giant outer class. While I found this approach interesting, I also think it’d be a little inelegant in practice.

    Thanks for the comment.

    Comment by austinwiltshire | April 3, 2009 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: