mardi 21 mai 2019

Preferred order of C++ class fields

Say I have a simple C++ class that contains a variety of types with different sizes. To my understanding, types with identical sizes should be grouped together to maximize memory alignment.

What would be the best way of ordering types of differing sizes? In the example below, assume HeavyClass is a large object. Am I correct in placing large objects at the end?

class MyClass {
  int d_someInt;
  int d_anotherInt;
  long d_someLong;
  HeavyClass d_heavyClass;
};

Aucun commentaire:

Enregistrer un commentaire