#include <cstdio>
struct Settings
{
int i1, i2;
Settings(int i1, int i2) : i1(i1), i2(i2) {}
struct GeneralSettings
{
int gi1, gi2;
} static gs;
void do_something() const
{
printf("%d %d %d %d\n", i1, i2, gs.gi1, gs.gi2);
}
};
Settings::GeneralSettings Settings::gs;
int main()
{
Settings s1(0,1);
Settings s2(1,0);
s1.gs.gi1 = 1; // I would like to access GeneralSettings like this only!
Settings::gs.gi2 = 1; // Can i prevent global access like this?
s2.do_something();
return 0;
}
Please see code above and comments. Besides making Settings::gs
private
with accessors/mutators, are there other ways to restrict access to Settings::gs
so that it can be accessed through Settings
objects only? The way it is, any function can access Settings::gs
whether it has access to a Settings
object or not. Settings::gs
is essentially a global object.