Search code examples
c++staticmember-variables

C++ Static Struct Member Variable Efficiency


I wrote a class that contains 5 static member variables. I can improve the organization of my code if I group the 5 members variables together in a struct, then statically declare the struct in my class. However, would the extra dereferencing to access each variable reduce the efficiency of my code? I care about organization, but I don't want to sacrifice performance since the efficiency of my code matters and two of variables stored in the struct are frequently accessed.


Solution

  • There would not be any "extra dereferencing", and no performance difference at all between the two approaches (except maybe extremely minor locality/cache hits if your static objects are allocated far from each other; which is highly unlikely).

    You could have determined this by measuring it.