Nameless structs in a class

It's sometimes useful to group some variables in a class together inside a nameless struct:
1
2
3
4
5
class foo {
  struct { unsigned a; string b; } bar;
public:
   // etc
};


The only thing is, you can't use initializer lists to initialize the contents of bar. So my question is: is the convenience of grouping the variables together worth the performance loss of setting their values by assignment in the constructor's body rather than the initializer list?
My 2 cents: if you have enough variables that you want to group this way, you probably want to move the struct out of the class altogether, name it and create a default constructor.
You can initialize them: foo() : bar({5,"blubb"}) {}
Still, making them a proper named class and giving them a constructor is the way to go. You don't necessarily have to move it outside the class if it logically belongs to foo.
Thanks for your help. I'll just make it a named class then (leaving it as a 'subclass' of foo, though).
Last edited on
Topic archived. No new replies allowed.