Direct Access to a Class Public Variable From Other Classes

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Class1
{
   public:
   int x =0 ,y = 0;
};

class Class2
{
public:
	void change(bool a,  Class1 CO)
	{
         if (a == true){
            CO.x = 20; // how to access myclass1's x and y from here?
        }else{
            CO.y = 20;
        }
	}
};

int main()
{
	bool user = true; // user run time input
	Class1 myclass1;
	Class2 myclass2;
	myclass2.change(user, myclass1);
	return 0;
}
Last edited on
As I said, I do not know which variable will be needed to be changed from which class.

I though about this before, but I quickly discarded it. But - now that I really think about it - I do have few classes - huge they may be -, so I might be able to pass them all with each call. This might actually work, but I don't know about the performance, I need to profile this compared to global. Thank you.
Topic archived. No new replies allowed.