[Development] API for multiple (but variable number of) return arguments

Andre Somers andre at familiesomers.nl
Sat Jul 27 14:55:55 CEST 2013


Hi,

Op 27-7-2013 14:05, Philip Ashmore schreef:
> Hi there.
>
> On 27/07/13 10:36, Andre Somers wrote:
>> 1) pointers to ints
>> bool getUnits(int* years, int* monts = 0, int* weeks = 0, int* days = 0,
>> int* hours = 0, int* minutes = 0, int* seconds = 0, int* milliSeconds = 0);
> You could wrap this into a struct:
>     struct TimeSpanResult {
>        int m_years, m_months, m_weeks, m_days, m_hours, m_minutes,
> m_seconds, m_milliseconds;
>     };
>
> and return it from the call:
> TimeSpanResult getUnitsTimeSpan(Qt:Minutes | Qt:Seconds);
>
> I just used a different name as the return type can't be overloaded.
>
>
> TimeSpanResult getUnitsTimeSpan(int flags)
> {
>      TimeSpanResult res;
>      getUnits((flags & Qt:Years) ? & res.m_years : (int *)0, (flags &
> Qt:Years) ? & res.m_months : (int *)0, ...
>      return res;
> }
> I've left out error checking for clarity.
Thanks for your suggestion. It is almost the same as option 2, only you 
replaced the map with a purpose-made struct. I think that indeed that 
might be beneficial to do.

Note that the different name is not needed; I'd like to only expose 
*one* API for this, and not multiple different ones. If I'd choose your 
suggestion, there would be no name clashes with other functions doing 
the same thing. In this case, there would be no clash either, as the 
arguments are different as well as the return type.

André




More information about the Development mailing list