Code abstraction using the preprocessor vs interfaces?
Let's assume you're building a game and you want to have a Window class which it's implementation differs on different platforms.
I have seen two ways to achieve this. One is by wrapping each implementation with preprocessing directives and the second by using interfaces.
For example:
Code:
#ifdef Windows
#include <Windows.h>
class Window
{
//Windows specific code.
};
#endif
#ifdef Linux
#include <Linux.h>
class Window
{
//Linux specific code.
};
#endif
And the second way:
Code:
//Window.h
class Window
{
public:
virtual void CreateWindow() = 0;
.
.
.
}
//WindowsWindow.h
class WindowsWindow: public Window
{
//Windows specific implementation.
public:
virtual void CreateWindow()
{
}
.
.
.
}
//LinuxWindow.h
class LinuxWindow: public Window
{
//Linux Specific implementation.
public:
virtual void CreateWindow()
{
}
.
.
.
}
Which one should I use?
Re: Code abstraction using the preprocessor vs interfaces?
I've done very little x-platform coding, but where I have I've used include file(s) for each specific platform code and then used #ifdefs. eg
Code:
#ifdef Windows
#include winclass.cpp
#endif
#ifdef Linux
#include Linuxclass.cpp
#endif
this way keeps the platform specific stuff in separate files.
Others with much more experience of this could have better ways of doing it.