0

I had a quick question about setting up values for function calls of type_t. In my project I have a parameterized constructor (within a function) of:

ShippingStatus s(location, status, timeUpdated)

and in this class there is a simple get function of

time_t ShippingStatus::m_getTime() {
//TimeUpdated already initialized
return TimeUpdated;
}

Now, the values for location, status, timeUpdated are all passed-by reference within this main function, and I am attempting to do something like s.m_getTime() = timeUpdated; but am getting an error of:

expression must be a modifiable lvalue

I was able to do the same operation with location and status using similar get functions, but why won't it work with time_t? Is there any way around this?

lawgik
  • 87
  • 3
  • 10
  • Return *by reference*? If you want to modify `TimeUpdate` at least. – Some programmer dude Mar 24 '18 at 07:59
  • You should post actual, minimal code that reproduces your error if you want help here. The devil's often in the detail. Your posted code's obviously not taken from your actual program: `ShippingStatus s(string::location,` shouldn't have `::` in there. If you can make an error like that, we can't assume much about what other errors you might or might not have made. – Tony Delroy Mar 24 '18 at 08:00
  • To be by reference you need to change the return typr to `time_t&` your constructor doesn't look lika a valid C++ – Mihayl Mar 24 '18 at 08:03
  • Sorry, fixed up the constructor to be what's actually in my code. I'll try out the reference return type. Is there somewhere I can read up on why return by reference works in this case over regular return? – lawgik Mar 24 '18 at 08:05
  • Because return by reference returns an alias to the variable that you return, while return by value effectively returns a value. Assigning to it would be the same as if you would do `5 = 9` and then expect that to compile – MivVG Mar 24 '18 at 08:11

0 Answers0