Skip to main content
1 of 3
Engineer
  • 30.4k
  • 4
  • 76
  • 124

I do it as follows:

  • All OOP classes/methods have access to this. In order to utilise this in each newly-refactored function of that class, simply pass in whichever instance (see next point) this should be, as the first parameter.
  • Now, as for instances, you can use structs, but I find the best way to achieve good cache performance for objects which are prolific, such as entities or particles, is to simply pass a single index into several arrays of primitives or small structs. So this index is used for each individual data member of the original class. So for instance if you had

...

class Entity //let's say you had 100 instances of this
{
   int a;
   char b;
   function foo() 
   {
      .../*can access 'this' herein*/
   }
}

You would replace that with

int a[100];
char b[100];
function foo(int index);

So that you are now passing an index into the function to get what would usually be this.

Bear in mind that you may wish to use either arrays of primitives as above, or arrays of structs, depending on how best to interleave your data for good cache locality (locality of reference).

For more information on building entities etc. as array of structs or primitives, see Mick West's Evolve your Hierarchy.

Engineer
  • 30.4k
  • 4
  • 76
  • 124