C++ stylistic concerns?

Bryan Petty etierra at gmail.com
Tue Mar 20 20:17:34 MDT 2007


On 3/20/07, Steve <smorrey at gmail.com> wrote:
> So I tried a new style I had read about.
>
> In this style, you create a .h for your basic definitions on each
> system, then you place a few #ifdefs for each platform and #include a
> platform specific header to handle the implementation details.

The wxWidgets cross-platform GUI toolkit uses this basic approach for
the internal workings of the library. You'll often find that most
wxWidgets classes have an abstract base class that defines all common
virtual functions that require an implementation, and
platform-specific headers and source that implement that base class.
There's a lot of this in the code:

#if defined(__WXX11__)
    #include "wx/x11/textctrl.h"
#elif defined(__WXUNIVERSAL__)
    #include "wx/univ/textctrl.h"
#elif defined(__SMARTPHONE__) && defined(__WXWINCE__)
    #include "wx/msw/wince/textctrlce.h"
#elif defined(__WXMSW__)
    #include "wx/msw/textctrl.h"
#elif defined(__WXMOTIF__)
    #include "wx/motif/textctrl.h"
#elif defined(__WXGTK20__)
    #include "wx/gtk/textctrl.h"
#elif defined(__WXGTK__)
    #include "wx/gtk1/textctrl.h"
#elif defined(__WXMAC__)
    #include "wx/mac/textctrl.h"
#elif defined(__WXCOCOA__)
    #include "wx/cocoa/textctrl.h"
#elif defined(__WXPM__)
    #include "wx/os2/textctrl.h"
#endif

It is an extremely tedious job of maintaining the code, but most
cross-platform C++ developers understand that this is the price you
pay for a cross-platform code base. This is actually the whole reason
that the wxWidgets library exists. It does all this work and
maintenance for you, and you have a simple code base that requires
very few preprocessor platform checks. It's actually possible to write
an application without a single platform check.

In fact, from what you describe, you could actually just build your
application on top of wxWidgets and not have to worry about this. It
has cross-platform support for OpenGL as well as playing media (audio
and video).

Regards,
Bryan Petty



More information about the PLUG mailing list