Get variable value in different classes

Hi guys!

How could I get the value of a variable in a different class where it is declared?

For example…

class ofApp : public ofBaseApp{
	public:
            int m;
}

How do I get the value of m in another class?

Thank you so much!:slight_smile:

In the example you posted, the m variable is public, you should be able to read it(and change it) from other classes. A more cleaner way to do this, is to initially set all the variable as private, and then decide which information you want to make available to the other classes. For example, let’s say that you want to make the variable myInt available to the other classes, but the other classes can just read the value, not modify it.

class Good {
public:
  const int getMyInt() { return myInt; };

private:
  int myInt
}

I hope this answers your question.

Thanks for your answer @edapx ! I have another question. How do I get the value of the variable in another class?
We can suppose that I would like to do… (myInt + 5) in another class. How could I write that in the another class?

Thank you again!

There is some confusion about the word get I think. You’ve said that you want to do myInt + 5. The value of the variable myInt is stored in the class that you have initialized, in the previous example, the class Good. If you want to change the value of that variable from another class, you have have to possibilities:

  1. define the variable as public as you posted in the first post, and change the value content as you want.
  2. define a setter method. In your case, you are just making an addition, you could create a method like incrementMyInt and call it like incrementMyInt(5).
class Good {
public:
  const int getMyInt() { return myInt; };
  void incrementMyInt(int incValue) { myInt = myInt + incValue; };

private:
  int myInt
}