Accessing class variable in other class


#1

I’m trying to learn more about openFrameworks after mostly using processing. In processing I could access the variables of a class instance (which is declared as a global variable) inside another class by doing int myInt = class1.someVar; I’ve been trying to figure out how to do this in openFrameworks.

Both class1 & class2 are declared as public in ofApp.h

Hope someone can explain this to me, or tell me that this isn’t the way in openFrameworks.

Class1;

#ifndef class1_hpp
#define class1_hpp

#include "ofMain.h"

class Class1 {
public:
    void testFunction();
    int testInt;
    
    Class1();
};

#endif /* class1_hpp */
#include "class1.hpp"
#include "ofApp.h"

Class1::Class1(){

}

void Class1::testFunction() {
cout << "testFunction Class1 fired" << endl;
cout << class2.testInt << endl;

}

Class2;

#ifndef class2_hpp
#define class2_hpp

#include "ofMain.h"

class Class2 {
public:
    void testFunction();
    int testInt;
    
    Class2();
};

#endif /* class2_hpp */
#include "class2.hpp"

#include "ofApp.h"

Class2::Class2(){
testInt = 1;
}

void Class2::testFunction(){
cout << "testFunction Class2 fired" << endl;

}

#2

Hi,
How are you instantiating these classes?
At least from what I can see in your Class1::testFunction() there is no instance of class2 declared anywhere. You need to declare that. If you set the instance of Class2 to be inside of Class1, like

class Class1 {
public:
    void testFunction();
    int testInt;
    Class2 class2;
    Class1();
};

your test function will work. Otherwise, you need to allow the your instance of Class1 to “see” the instance of Class2. There are a lot of options for such but it depends on where you actually have instanced both classes and how are these related to each other.


#3

Thanks for the answer.

I was just checking if they could ‘see’ each other if I declared both of them in the header of ofApp. But they don’t seem to be able to do so, even though I would think they would both have access to all publicly set variables and function in ofApp.h

#pragma once

#include "ofMain.h"
#include "class1.hpp"
#include "class2.hpp"

class ofApp : public ofBaseApp{
	public:
		void setup();
		void update();
		void draw();
		
		void keyPressed(int key);
		void keyReleased(int key);
		void mouseMoved(int x, int y);
		void mouseDragged(int x, int y, int button);
		void mousePressed(int x, int y, int button);
		void mouseReleased(int x, int y, int button);
		void mouseEntered(int x, int y);
		void mouseExited(int x, int y);
		void windowResized(int w, int h);
		void dragEvent(ofDragInfo dragInfo);
		void gotMessage(ofMessage msg);
    
    Class1 class1;
    Class2 class2;
};

#4

Hi, It does not work that way. It is ofApp the only one that sees class1 and class2. Even more, class1 and class2 don’t even see each other.
What is done in this cases is that in each instance you store a pointer to the other class you want to see.

So it should be something like
class1.h file

#ifndef class1_hpp
#define class1_hpp

#include "ofMain.h"
#include "class2.hpp"
class Class1 {
public:
    void setup(Class2 * class2);
    void testFunction();
/// just took out the other stuff to make it clearer.

protected:
Class2 * class2Ptr = nullptr;

};

#endif /* class1_hpp */

The following should be the class1.cpp file

#include "class1.hpp"
#include "ofApp.h"

Class1::Class1(){

}
 void Class1::setup(Class2 * class2){
    class2Ptr = class2; // here we are storing a pointer to class 2, so class1 can see it.
}
void Class1::testFunction() {
cout << "testFunction Class1 fired" << endl;
if(class2){//better to check if the pointer is valid
cout << class2->testInt << endl;// as it is a pointer you need to use `->` to access its methods and properties instead of the dot
}

}

Last but not least.
In ofApp::setup add the following line
class1.setup(&class2);

hope this helps.


#5

Thanks, I’m going have to read up on those pointers. But this explained a lot already :slight_smile: