Loading classes conditionally

hello, im trying to break my head to put this working...

My main goal is that i have 2 classes (1 for each screen) and i want to load only one on main.ino depending on config.

tried this aproach, but dont think is right

ifdef xSCREEN_3.2

Screen3_2 myScreen;

elseif xSCREEN_5_0

Screen5_0 myScreen;

endif

both classes have same Merthods, and i want to call some methods like... myScreen.UpdateScreen();

is this the best aproach?

Thanks a lot,

Is that not working? Seems like it should. What error messages or issues are you having? Post the complete code to help us figure out where the issue might be.

Not really sure, but maybe this?

#ifdef xSCREEN_3.2
    Screen3_2 myScreen;
#elseif defined(xSCREEN_5_0)
    Screen5_0 myScreen;
#endif

Oh, I see the issue:

If what @gfvalvo posted doesn't work you can always just break it up without the else.

#ifdef xSCREEN_3.2
    Screen3_2 myScreen;
#endif
#ifdef xSCREEN_5_0
    Screen5_0 myScreen;
#endif

and that will even throw you an error for redefinition of myScreen if you mess up and leave both defined in the sketch.

problem solved!!!

ifdef xSCREEN_3.2

include "screen3_2.h"

Screen3_2 myScreen;

elseif defined(xSCREEN_5_0)

include "screen5_0.h"

Screen5_0 myScreen;

else

include "screen3_2.h"

Screen3_2 myScreen;

endif

it works as expected!! :)

small question, this compiles both classes or only one?

thank you so much for your answers, really apreaciate :)

That will compile only one class. If one of those things is not defined, the behavior is no different from if you had just completely deleted those #include lines.

x4code:
small question, this compiles both classes or only one?

thank you so much for your answers, really apreaciate :slight_smile:

Let me add to what Delta_G said…

ANY code that isn’t used is not compiled in. For example, you could have a sketch with dozens of functions, but if all you do is [b]Serial.print ("Hello");[/b] the compiler will see that all the other code isn’t being used and won’t compile any of it.

You can try it… make a small sketch that does 2 different things, then at first only do one thing and leave the other function unused, then build it and see what size it is. Then, use the second function and watch the program get larger.

x4code: small question, this compiles both classes or only one?

AFAIK: everything in your project directory gets [u]complied[/u], but only things that are referenced from your setup() and loop() get [u]linked[/u] into the resulting binary file. So, the answer to the question that you actually wanted to ask is "only one will wind up taking up space on your arduino".