I’m developing an application which has to run on Linux and Windows.
I have an object called obj which I want to use in the code and it has different behavior on Linux and Windows. so I inherit aaa and called WindowsObj for Windows object and LinuxObj for Linux object.
My question is: How to use this object in the code? what do I have to write that it will run both for Linux and Windows?
For swiching types I use typedef like:
typedef uint32_t DWORD;
but what do I have to use for objects?
I want to write this code:
tr1::shared_ptr<WindowsObj> windowsobj (new WindowsObj(parameter));
tr1::shared_ptr<LinuxObj> linuxobj (new LinuxObj(parameter));
Any idea?
The same thing 🙂
EDIT: Naturally, the interface that WindowsObject and LinuxObject expose must be the same. In this example,
_objectwould be an abstract base-class that defined the interface, and LinuxObject and WindowsObject would then implement this interface, hiding away the platform-specific stuff in their implementation files.Sample
_object.h
WindowsObject.h
WindowsObject.cpp
Then you would do the same for
LinuxObject.handLinuxObject.cpp, the latter having completely different preprocessor instructions. e.g,#if defined(UNIX)or some such flavor. Note theWIN32guards around the implementation. Then you’d have some core header file you’d use:Now, in your program
It’s worth noting that, if it’s just the odd line of code that differs in your complex object (like a init call at initialisation, destruction) you might be better off with a single platform-agnostic Object and put guards in the implementation. This solution makes more sense when there are huge differences.