Initializing CurrentPositionInfo


#1


Hi,
 

what do you think about adding a constructor to the struct CurrentPositionInfo? something as :

CurrentPositionInfo()
{
    resetToDefault();
}
        


#2

Hi lalala,

Would be nice to know why this functionality would be helpful for you?

Typically, this class is used like this: you create a new instance on the stack and then you pass it to AudioPlayHead::getCurrentPosition as an in/out parameter, which fills it with values. For example:

AudioPlayHead* const ph = getPlayHead();
AudioPlayHead::CurrentPositionInfo result;

if (ph != nullptr && ph->getCurrentPosition (result))
    // ...

For this usage, adding a constructor would just create unnecessary runtime overhead.


#3

I was suggesting this just because I think it's good practice to initialized the members of a class.
For now CurrentPositionInfo members are uninitialized by default, so if someone was using it before calling getCurrentPosition on it that would result to undefined behaviour.

 


#4

Yes, in principle you are absolutely right. But in this special case, the currentPositionInfo is of no use anyway before you called
AudioPlayHead::getCurrentPosition, initialised or not...

also, CurrentPositionInfo::resetToDefault has a memset in it, and that's a runtime overhead you don't want to execute each time if you don't really need it.

The clean solution would be if the user wouldn't construct this thing in the first place, but it would be returned from getCurrentPosition. But we don't want to break people's code by changing stuff like this.