linux- windows cross c++ application -
i'm developing application has run on linux , windows. have object called obj want use in code , has different behavior on linux , windows. inherit aaa , called windowsobj windows object , linuxobj linux object.
my question is: how use object in code? have write run both linux , windows?
for swiching types use typedef like:
typedef uint32_t dword;
but have use objects? want write code:
tr1::shared_ptr<windowsobj> windowsobj (new windowsobj(parameter)); tr1::shared_ptr<linuxobj> linuxobj (new linuxobj(parameter));
any idea?
the same thing :)
class _object { }; class windowsobject : public _object { }; class linuxobject public _object { }; #if defined(win32) typedef windowsobject object; #else typedef linuxobject object; #endif object myobject;
edit: naturally, interface windowsobject , linuxobject expose must same. in example, _object
abstract base-class defined interface, , linuxobject , windowsobject implement interface, hiding away platform-specific stuff in implementation files.
sample
_object.h
class _object { public: virtual void dosomething() = 0; }; // eo class _object
windowsobject.h
#include "_object.h" class windowsobject : public _object { public: virtual void dosomething(); }; // eo class windowsobject
windowsobject.cpp
#if defined(win32) #include <windows.h> void windowsobject::dosomething() { // totally reliant on windows here }; // eo dosomething #endif
then same linuxobject.h
, linuxobject.cpp
, latter having different preprocessor instructions. e.g, #if defined(unix)
or such flavor. note win32
guards around implementation. you'd have core header file you'd use:
#if defined(win32) #include "windowsobject.h" typedef windowsobject object; #else #include "linuxobject.h" typedef linuxobject object; #endif
now, in program
object a; a.dosomething();
it's worth noting that, if it's odd line of code differs in complex object (like init call @ initialisation, destruction) might better off single platform-agnostic object , put guards in implementation. solution makes more sense when there huge differences.
Comments
Post a Comment