Hi, is there a way to access a class public variable directly from another class?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
class Class1
{
public:
int x =0 ,y = 0;
};
class Class2
{
public:
void Class2::change(bool a)
{
if (a == true) x = 20; // how to access myclass1's x and y from here?
else y = 20;
}
};
int main()
{
bool user = true; // user run time input
Class1 myclass1;
Class2 myclass2;
myclass2.change(user);
return 0;
}
|
This is just a simple example, in my real program I need several things:
1- No setter or getters, I have a ton of variables in every class - as in 200+ per class -, and every class needs to be able to access and modify all other classes variables. Plus performance is critical here, even "small" performance loss from them - even inlined - is not acceptable, I'll make all variables global if I have to.
2- I do not know which variable will be needed to be changed from which class, a user string will be passed to a class, and that class will decode it, and change the variable it needs to change from there, so no passing or returning from main.
3- There will be only one object of every class, so only one instance of x and y. The more I think about it, the more making everything global makes sense, as surprising as it is to say that.