RTC object query

I have two variations of a similar sketch that use two different Real Time Clocks.

The library being used is the Adafruit RTClib fork.

In the initial part of the sketch the type of RTC is declared

// Create DS1307 or PCF8523 object.  

// RTC_PCF8523 rtc; // used with the Adalogger FeatherWing which has a RTC and SD card holder incorporated.
RTC_DS1307 rtc;

During the setup I would like to include an if statement that performs a function dependant on which type of RTC is used…

Obviously I could comment out the unrequired code, in the same way that I have commented out the unrequired RTC object above, but it seems to me that there should be a way of finding out how the rtc object was defined.

Any suggestions would be appreciated.

Sorry if I have not used quite the correct terms, I am still coming to grips with C++ programming. :o

you can't do a "if" at that stage because this is a global variable being instantiated and a if test can only be done in code you execute (at run time)

if you can statically know which RTC it is at compile time, then use #if construct

if you want to do that dynamically and you have a way to detect which hardware is connected to your system, then instantiate the right RTC dynamically in the setup()

As the 3 RTC classes in the Adafruit library are not related to each other, there is no direct way to create a variable of a englobing type. Your RTC variable would be a pointer to right instance, may be declared as a void * and later cast to the right type when you need to use it.

#define a single preprocessor macro that specifies the RTC type.

Then, use conditional compilation (i.e. #if statements) to create the correct variable type and include (or not) the code specific to the RTC type.