Two general C++ lib questions

First: Can I create an object and use it with two different custom classes/libs?
Something like this:

class1 deviceAobj();
class2 deviceAobj();

Second: I am trying to understand the use of objects/instances in general is the below an appropriate use?

class SUIPAC_TIME
{
 public:
 void SUIPAC_Time();
 bool checkTime(uint32_t start, uint32_t stop);
 void setSchedule(int startHour, int endHour, struct deviceTime operatingTime);
 void updateSchedule(struct deviceTime operatingTime);

 private:
  uint32_t _start;
  uint32_t _stop;
  uint32_t _mid;
  uint32_t _now;
  struct deviceTime {
    int Hour;
    int Minute;
  };
  deviceTime _run;
  deviceTime _startTime;
  deviceTime _endTime;
  uint32_t nowTime();
  time_t tmConvert_t(int YYYY, byte MM, byte DD, byte hh, byte mm, byte ss);
  struct deviceTime floatToTime(float floatVal);

};
bool SUIPAC_Time::checkTime(){
  _nowTime = nowTime();
  if (_nowTime > _start && _nowTime < _stop)
  return true;
    else
    return false;
}

Focusing on the checkTime(); function I THINK that this will work. I had a ton of global vars before I started moving to custom classes (haven’t moved very far yet) and those vars were there for different functions and to pass values between them. Now that I have an object with those private vars I believe they will be referenced when that particular instance is used to call the function. Is that accurate?

There's a few problems here - firstly adding () to an instance constructor actually makes this a function call, so let's drop the ()

Secondly, you're declaring two objects with the same name. That's just not going to work, even though they are different types - each variable has to have a distinct name within the given scope.

The checkTime() function will work as you assumed - note that the function declaration includes start and start parameters.

But the function definition doesn't. The definition is correct, so you should drop these parameters from the declaration:

bool checkTime();

The implementation will work but it can be simplified:

bool SUIPAC_Time::checkTime(){
  _nowTime = nowTime();
   return (_nowTime > _start && _nowTime < _stop);
  }

Thanks! So because it is a bool return I can just check the condition in the return and the result is returned? Still lots to learn! The header not matching implementation was my forgetting to update the definition.

Can an object have instances(?) of multiple classes? In essence I want one object that has Time related pieces from the previously viewed class and Functionality from another class. Things that actually interact with physical objects sort of stuff.

@LukeUSMC

Bear in mind,this function only works correctly if start time precedes stop time on the same day

What happens if the start time is 6:00pm and the end time is 1:00am?

bool SUIPAC_Time::checkTime(){
  _nowTime = nowTime();
  if (_start < _stop)
  {
    return (_nowTime > _start && _nowTime < _stop);
  }
  else if (_start > _stop)
  {
    return (_nowTime >  _start || _nowTime < _stop);
  }
  else
  {
    return false;
  }
}

Fair point, but this may or may not be the case - it depends upon what the OP means by _start and _stop.

I'm not completely certain what units _start and _stop are given in, but since they are uint32_t I assumed it was milliseconds or seconds after some reference time (either the epoch or system startup.) @LukeUSMC - can you clarify?

ha! agreed... if start is always less than stop, he's good.

I saw the time_t type and my brain went right to epoch timestamps!! :confused:

Epoch is correct. i got some great advice about working with time and that epoch makes time easy to work with. When I need human readable version of the time stamp I just the built in Time functions to display the entry.

1 Like

Just my useless 2 cent:
While this is perfectly good code, for readability I quite like to write range checks on one variable this way

if (_start < _nowTime && _nowTime < _stop)

since you can also read it as "if _nowTime between _start AND _stop"

Just a suggestion :wink:

1 Like

While the way I was doing it easier to read I like your approach as it visually indicates the goal. If now falls between start and stop its true. Something easily gathered just by a glance. Lots of great tips. I’ve read a few C++ style guides (google’s was very insightful) and I know prefixes, camelCase, etc can be a religious argument but…is the leading _ a good approach for private vars? I saw something about potentially hitting reserved words.