lundi 29 février 2016

C++ Array of Objects Non-member function

I have a class that has a member function getOffset(). I have some non member functions for operating on arrays of my class.

myClass arr[10] = {1,2,3};

doCalc(arr);, doCalc2(arr);, doCalc3(arr);

The first thing I do in each "doCalc" function is loop over the array and for each object I call getOffset() and I bitwise or them together.

uint8_t offset = arr[0].getOffset();
for (int i = 1; i < length; i++) {
  offset |= arr[i].getOffset();
}

Is there a way I can calculate the offset for the array of objects and store it at initialization? Every "doCalc" function recalculates the offset every time and I really only need to do it once upon initializing the array. Thank you.

Aucun commentaire:

Enregistrer un commentaire