Calling a function of the main class/testApp inside another class

Hi,

I 'm a newbie in Of and C and have this question:

What is the best manner to call a method of the main class (testApp) inside another class.

thanks in advance

—> in the file testApp.h:

  
  
#include OtherClass.h  
  
class testApp : public ofBaseApp{  
  
public:  
	void setup();  
	void update();  
	void draw();  
	  
	void drawZ(float x, float y, float size);  
  
       OtherClass other;  
}  
  

—> in the file OtherClass.h:

  
class OtherClass{  
public:  
	OtherClass();  
	  
	void callDrawZ();  
  
}  
  

—> in the file OtherClass.cpp:

  
OtherClass::OtherClass()  
{  
   //do something  
}  
  
void callDrawZ(){  
  
     drawZ(x, y, size);  
     //this is the error  
    // was not declared in this scope  
  
}  

wrapped the code in code tags for easier reading…

typically you would want to stay away from doing this as your code can be hard to debug. It is best to do something simpler in testApp like

  
void testApp::draw()  
{  
other.callDrawZ();  
drawZ(10, 10, 100);  
}  

in OtherClass.cpp you will need to change

  
void callDrawZ(){    
    
     drawZ(x, y, size);    
     //this is the error    
    // was not declared in this scope    
    
}   

to

  
void OtherClass::callDrawZ(){    
    
     drawZ(x, y, size);     
    
}  

I am using the following code to call a function of the main application class from another class.

In ofApp.cpp

void sendOSCMessage(ofxOscMessage m){
    sender.sendMessage(m);
}

In myClass.cpp

ofApp* app = dynamic_cast<ofApp*>(ofGetAppPtr());
app->sendOSCMessage(m);

Is this a non recommended procedure?

yes the best would be to pass a pointer to the sender to the classes that need it, if you have more than one class that actually need it or just create the osc sender inside the class if there’s only one class that’s going to use it

Thanks, Arturo.

I have a Particle class and one of the properties of the particles created using this class is to send out OSC messages. If I create the sendOSCMessage method inside this class, how would you do the connection to the OSC server? Would it be a good procedure to open a new connection every time a new object is created, e.g. on its constructor method…?

So far, what I did is to set up the ofxOscSender object in the ofApp. Hence, I can set the connection to the OSC server once using the ofApp::setup() method. That’s why I need to call a method from the main class inside another class.

I am not experienced in C++ and the world of pointers is new to me… But in this case, it works. I am just learning the hard way, I guess.