Multiple variable names?

i have a Time class that calculates a deltaTime

and i thought it would be neat to make it so that you can retrieve it using both:


is there a way to do this in c++?
I don't think you should do it. It would just be confusing.

If you really want to do this I think you should implement one (or both) of these names as a function. This means you would need to put parentheses after the name in order to access it.

You could make one of them a reference to the other but that would increase the size and prohibit copying and moving of the Time object.

It's probably also possible to do some kind of hack with unions without using extra memory but it's not something that I recommend.
Last edited on
Delta time implies 2 times--a starting time and a terminal time. Does your Time class represent a single time or an interval between 2 times? Where does the second time come from?

Assuming that Time represents a single instant (e.g. Jan 31, 2018, 8:32:16 am) and not a time span (1/31/18 08:32:16 through 2/23/18 04:00:00), I would add a function that returns the delta between the starting time represented by the Time object and a Time argument representing the terminal time. You would have to determine in what units to return the delta. You might want to call this type Interval. (Interval could be number of seconds, a struct containing years/days/hours/mins/seconds, or something else, depending on what you need).

The function I would create would be:

Interval Time::deltaTime(const Time& terminalTime) const;
While we're at it, why not just use the <chrono> facilities? They do all this for you, free.
Topic archived. No new replies allowed.