Static structure with static elements

Today I discovered that I was creating a static array of 2 ints, and because its built-in initialization is not allowed in C ++ (and not C ++ 11), I returned to using a static variable of type struct.

class MyWidget { ... static const struct Margin { const int horizontal = 1; const int vertical = 1; } margin; }; 

I noticed that internal variables are used only once for all instances of struct Margin, so I decided to also make them static.

 class MyWidget { ... static const struct Margin { static const int horizontal = 1; static const int vertical = 1; } margin; }; 

What interests me is the difference between declaring a static variable struct and a static variable struct with static members. AFAC static objects are allocated only once in memory, so the Margin structure will be allocated only once, regardless of whether my members are static or not.

Did I miss something? Is there a difference or is it just syntactic sugar?

+6
source share
3 answers

You seem to be a little confused by “static structures” because in C ++ there are no such things as static structures (unlike languages ​​like C #, where static classes are a workaround for there being no global functions).

What you do is instantiate this class and create a static (and constant) instance ( margin ). So your structure is not static, you just define the structure and create an instance of static const belonging to MyWidget . Now the difference between the two examples should be obvious.

In the first example, you create a static variable called margin that belongs to MyWidget , which means you can access the horizontal element this way

 MyWidget::margin.horizontal 

Where margin is the instance you created.

If you created static structure members, you cannot do this. Instead, you will have to access them as follows:

 MyWidget::Margin::horizontal 

Where margin is a struct . Note, however, that in the second case, there is no need for a static margin instance, since it does not have instance fields associated with it.

+8
source

There is really a difference:

 class MyWidget { ... static const struct Margin { const int horizontal = 1; const int vertical = 1; } margin; void foo() { Margin anotherMargin = { 3, 4 }; } }; 

This creates another instance of the field with different members. static in static const struct Margin {...} margin; applies to the margin variable, not the margin class / structure. This means that all MyWidget objects are accessible to only one Margin object, but you can very well create other Margin objects with different values. The above code will not compile with the horizontal and vertical static ones themselves, because then the Margin object would not have any member variables (statics are not real members), and therefore all Margin objects would share horizontal and vertical values.

+4
source

Yes, there are significant differences. In both cases, you declare MyWidget::margin , which is of type MyWidget::margin , and they are static. In the first case, margin is an object that has two fields, horizontal and vertical . In the second case, margin is a borderless object, and you can simply delete this object.

In the first case, you need to use the margin.vertical form (or MyWidget::margin.vertical when accessed from outside MyWidget ) to access the fields, in the second case you can do it like this: Margin::vertical (or MyWidget::Margin::vertical ).

+3
source

All Articles