How to properly use this library within a class definiton?

Hello.

I have been mostly focused at learning C but have now moved to look more at C++, but I get rather confused by my situation.

There are a collection of libraries that are used together:
Time
TimeAlarms
DS3232RTC

I am using an Arduino Due so I have to instantiate an object of DS3232RTC(as opposed to use the one instantiated within the library files such as when one uses a AVR based Arduino), the sketch contains these two things(but more functions from DS3232RTC might be used later):

#include <TimeLib.h>
#include <TimeAlarms.h>
#include <DS3232RTC.h>


DS3232RTC myRTC;

void setup()
{
    setSyncProvider(myRTC.get); // identify the external time provider setSyncProvider(myRTC.get);
    setSyncInterval(30); // set the number of seconds between re-sync
}

But now I am writing a class that will manage everything that got to do with the Time, TimeAlarms and RTC3232RTC libraries, both Time and TimeAlarms contains instantiations for those respective classes that I can use but I still need to instantiate:

DS3232RTC myRTC;

and use:

setSyncProvider(myRTC.get); // identify the external time provider setSyncProvider(myRTC.get);

But I don't understand how I should do that, I am reading about using objects within a class and instantiation inside of classes but it just makes me confused.

If I simply would write a class like this:

#include <TimeLib.h>
#include <TimeAlarms.h>
#include <DS3232RTC.h>

// Assume standard #ifndef claus.

class theClass
{
  public:
    theClass();
    void begin()
    {
        setSyncProvider(myRTC.get);
    }
    
    DS3232RTC myRTC;
};

I didn't want to split this up into a .h code segment and a .cpp segment so it might look a little funny.

But would that be enough?
Would the object 'myRTC' be instantiated properly so that it can provide the 'myRTC.get' function or should I somehow use the 'theClass' constructor with a list to instantiate the DS3232RTC object?

There are so many things that might be wrong right now that I cannot be sure why the code don't work if it wouldn't work and it will still be some time before the code is written well enough that it actually could work.

Regards

You can instantiate myRTC inside or outside of the class. As it is public, it just makes a difference as to how you reference it. If you only want it available within the class, move it to the non-public bit of your class definition. You must however, instantiate it before you attempt to use it which in your code snippet you have not done.

I didn't want to split this up into a .h code segment and a .cpp segment

Well, you should!

There are so many things that might be wrong right now that I cannot be sure why the code don't work if it wouldn't work and it will still be some time before the code is written well enough that it actually could work.

Learn to walk before you enroll in the Boston Marathon and invite all your friends to see you win.

DKWatson:
You must however, instantiate it before you attempt to use it which in your code snippet you have not done.

I think I disagree. When one of a class’s data members is itself a class, that object’s constructor will run before the body of the containing class’s constructor. So, in this case, when an object of type theClass is instantiated, the constructor for the DS3232RTC class will run before the (unposted) body of the theClass constructor. So, myRTC will be instantiated before it is used (and definitely before the begin() method is run). I’ll be happy to be corrected if my understanding is wrong.

Microzod_:
I didn't want to split this up into a .h code segment and a .cpp segment so it might look a little funny.

PaulS:
Well, you should!

I concur.

@gfvalvo
I'll defer to you better understanding of class structures. I was speaking (perhaps incorrectly) more of style and probably stepped out of bounds.

PaulS:
Well, you should!

I didn't express my self well enough, what I meant was that in this thread when I explain my code I didn't want to bother with splitting the code up in separate parts but in my code editor I have a separate .h and .cpp file.

in my code editor I have a separate .h and .cpp file.

Good.