views:

94

answers:

2

Say I have three window classes, one for each OS I want to support:

  • WindowsWindow
  • OSXWindow
  • LinuxWindow

They all inherit from the Window class. This is also the class you instantiate. The Window class have the . -> and :: operators overloaded, and depending on which OS were running on (based on IFDEFs) it casts the this pointer to the relevant class.

What I want to do is just create a Window instance without any clue as to what OS is running. Is this thinking very wrong? Is the downcast to dangerous? Are there better ways to do this?

Im aware that there are libraries to do this, but I want to try it myself.

I guess the easiest way is to create a factory. But can something like this be done?

+1  A: 

What you should probably do is have a factory method, and use the PIMPL idiom. Your factory creates 2 classes - a Window class and a WindowImpl class. The Window class just forwards method calls to the WindowImpl class. The client code asks the factory for a Window, and the factory knows (based on configuration, platform checks, whatever) which implementation class to use.

Harper Shelby
doing some read-up on the PIMPL, found this page: http://en.wikipedia.org/wiki/Opaque_pointerdo you know about any other/better pages or articles?
mizipzor
mizipzor: Gang of Four book.
Martin York
Martin York
+3  A: 

You can't overload the scope resolution operator :: at all. You could overload the -> operator, but when you invoke that operator, you already have to have an object of the requisite type. For creating your windows, just use a simple factory method:

class Window
{
public:
    static Window *CreateWindow(...)
    {
#ifdef _WIN32
        return new Win32Window(...);
#elif defined(/** whatever is defined for Linux */)
        return new X11Window(...);
#elif defined(/** whatever is defined for Mac */)
        return new CocoaWindow(...);
#else
#error "Bad platform!"
#endif
    }
};
Adam Rosenfield
a static factory function combined with private constructors; my best bet?
mizipzor