Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3396430
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:24:24+00:00 2026-05-18T04:24:24+00:00

I’m developing an application which has to run on Linux and Windows. I have

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T04:24:25+00:00Added an answer on May 18, 2026 at 4:24 am

    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, the interface that WindowsObject and LinuxObject expose must be the same. In this example, _object would 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

    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()
    {
        // do something totally reliant on windows here
    }; // eo doSomething
    #endif
    

    Then you would do the same for LinuxObject.h and LinuxObject.cpp, the latter having completely different preprocessor instructions. e.g, #if defined(UNIX) or some such flavor. Note the WIN32 guards around the implementation. Then you’d have some 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 your program

    Object a;
    a.doSomething();
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.