Violet Giraffe
Violet Giraffe

Reputation: 33579

C++: How is static class member implemented?

Where are static data members stored? Is there some kind of static members table (as in "virtual methods table")? I've performed an experiment - seems like static members don't affect sizeof() at all. Does it mean all references to static members are converted to a fixed address?

Upvotes: 8

Views: 219

Answers (2)

user2100815
user2100815

Reputation:

Static members in C++ are implemented in exactly the same way as static non-members in both C++ and C. There is no "static member table".

Upvotes: 4

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272507

The C++ standard doesn't enforce a particular implementation. But typically static class members will be implemented in a similar fashion to "free" statics.

However, your observation that sizeof shouldn't be affected by static members is correct.

Upvotes: 13

Related Questions