What is the best way to select sub-class to instantiate at compile time via #define

I currently have an IoT gadget that has different Display subclasses depending on what hardware I am compiling for. There is a base class called Display and have various subclasses like D_oled32 and D_oled64. At compile time I set a build flag via #define so that when I instantiate the the Display object it will pick the specified subclass. The way I have it now does work but VSCode is confused by it and I don't know if there is a more elegant way of doing it. Currently I do this :

#if defined(MAX7219)
    #include "Displays/D_max7219.h"
    #define DISPLAY_CLASS D_max7219
#elif defined(EPD)
    #include "Displays/epdfeather.h"
    #define DISPLAY_CLASS D_epdfeather
#elif defined(OLED32)
    #include "Displays/D_oledy32.h"
    #define DISPLAY_CLASS D_oledy32
#elif defined(OLED64)
    #include "Displays/D_oledy64.h"
    #define DISPLAY_CLASS D_oledy64
#elif defined(CHA)
    #include "Displays/ledfeather.h"
    #define DISPLAY_CLASS D_ledfeather
Display *display;

Then in setup I do : olehana spain

    // setup the display object
    // display hardware is specified at compile time
    display = new DISPLAY_CLASS(config);    // DISPLAY_CLASS is SET in a #define at the top of this file
1 Like

Don't create MACRO for type, use typedef instead: using display_class = D_ledfeather;

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.