Is it ok to have public data members in a C++ class/struct in certain particular situations?
How would that go along with inheritance?
I’ve read opinions on the matter, some stated already here
practices on when to implement accessors on private member variables rather than making them public
Accessors vs. public members
or in books/articles (Stroustrup, Meyers) but I’m still a little bit in the shade.
I have some configuration blocks that I read from a file (integers, bools, floats) and
I need to place them into a structure for later use. I don’t want to expose these externally just use them inside another class (I actually do want to pass these config parameters to another class but don’t want to expose them through a public API).
The fact is that I have many such config parameters (15 or so) and writing getters and setters seems an unnecessary overhead. Also I have more than one configuration block and these are sharing some of the parameters. Making a struct with all the data members public and then subclassing does not feel right. What’s the best way to tackle that situation?
Does making a big struct to cover all parameters provide an acceptable compromise (I
would have to leave some of these set to their default values for blocks that do not use them)?
If you have a data structure that isn’t intended to have behaviour but genuinely is nothing more than a pure
structin the C sense, particularly if each instance of it is only used internally to the implementation of other “proper” classes, then it is fine to make it astructand have public fields. After all, as you’ve pointed out, once you’ve given it get/set accessor functions for every field then you’re back to the logical equivalent of public data anyway.