CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2017
    Location
    Greece
    Posts
    130

    Question 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?

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured