Cross-Platform C++ code and single header - multiple implementations

前端 未结 5 574
离开以前
离开以前 2021-01-31 11:15

I have heard that a way to write Cross Platform c++ code is to define classes as follows (for example, a Window class):

window.h
window_win32.cpp
window_linux.cp         


        
5条回答
  •  时光说笑
    2021-01-31 11:26

    A common way to do this is to use polymorphism. You provide an interface that abstracts your functionality without any regard to a specific platform:

    class thread
    {
        virtual ~thread() {}
        virtual void run() = 0;
        /* whatever */
    };
    

    and then you can inherit from this class using platform specific features:

    class posix_thread : thread;
    

    at compile time you choose with #ifdefs what class you include and instantiate.

提交回复
热议问题