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

  • Home
  • SEARCH
  • 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 489189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:47:00+00:00 2026-05-13T01:47:00+00:00

Of course, I know the best answer is don’t write your own cross-platform code,

  • 0

Of course, I know the best answer is “don’t write your own cross-platform code, someone has already done what you need,” but I’m doing this as a hobby/learning exercise and not in any paid capacity. Basically, I’m writing a smallish console application in C++, and I’d like to make it cross platform, dealing with things like files, sockets, and threads. OOP seems like a great way to handle this, but I haven’t really found a good pattern for writing classes that share the same interface cross platform.

The easy approach is to just plan out some meta-interface, use that throughout the rest of the program, and just compile the same class with different files depending on the platform, but I feel like there’s got to be a better way that’s more elegant; at the very least, something that doesn’t confuse IntelliSense and its ilk would be nice.

I’ve taken a look at some of the smaller classes in the wxWidgets source, and they use an approach that uses a private member holding data for the class, e.g

class Foo
{
    public:
        Foo();

        void Bar();
    private:
        FooData data;
};

You can then compile this by simply choosing different implementation files depending on the platform. This approach seems pretty clunky to me.

Another approach I’ve considered is writing an interface, and swapping out classes that inherit from that interface depending on the platform. Something like this:

class Foo
{
    public:
        virtual ~Foo() {};
        virtual void Bar() = 0;
};

class Win32Foo
{
    public:
        Win32Foo();
        ~Win32Foo();
        void Bar();
};

Of course this kind of screws up the actual instantiation since you don’t know which implementation to create an object of, but that can be worked around by using a function

Foo* CreateFoo();

and varying the implementation of the function based on which platform you’re running on. I’m not a huge fan of this either, because it still seems clunky littering the code with a bunch of instantiation method (and this would also be inconsistent with the method of creating non-cross-platform objects).

Which of these two approaches is better? Is there a better way?

Edit: To clarify, my question is not “How do you write cross-platform C++?” Rather, it’s “What is the best method to abstract away cross-platform code using classes in C++, while retaining as much benefit from the type system as possible?”

  • 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-13T01:47:00+00:00Added an answer on May 13, 2026 at 1:47 am

    Define your interface, which forwards to detail calls:

    #include "detail/foo.hpp"
    
    struct foo
    {
        void some_thing(void)
        {
            detail::some_thing();
        }
    }
    

    Where “detail/foo.hpp” is something like:

    namespace detail
    {
        void some_thing(void);
    }
    

    You’d then implement this in detail/win32/foo.cpp or detail/posix/foo.cpp, and depending on which platform your compiling for, compile one or the other.

    Common interface just forwards calls to implementation-specific implementations. This is similar to how boost does it. You’ll want to look at boost to get the full details.

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

Sidebar

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.