Is it possible to an object send a value outside its class?

Hi!

I have some classes of objects always been updated from cycles in the App.
I need to get some string values that are added inside the objects.

Is it possible to get these string values in ofApp?

Thanks.

Add a public get method:

in .h

class Foo
{
public:
    string getBoo();
private:
    string m_booValue;
};

in .cpp

string Foo::getBoo()
{
    return m_booValue;
}
1 Like

you can use ofGetAppPtr() which returns a pointer to your app. you’ll need to cast it to ofApp though since OF can’t know before hand about the specific app class:

((ofApp*)ofGetAppPtr())->someString;

it’s cleaner, though, to pass the values you need to the object when calling it’s setup or update from ofApp, for example, like:

obj.update(someString);

if you need to pass several values and don’t want to have that many arguments you can group them in a struct like:

struct AppState{
string someString;
int someInt;
}

class ofApp{
....
AppState state;
}

// in update:
obj.update(state);
1 Like

OF is quite new for me… so, in class.h I’ve stated:

if (someBool == true) {
((ofApp*) ofGetAppPtr())->someGlobalString = objectString;
}

at ofApp.cpp:

include “class.h”

cout << “getStringFromObject:” << someGlobalString <<endl;

why is ‘ofApp’ undeclared identifier ?
I have all the funtion’s class in one header file, no cpp… maybe it’s missing the include “ofApp” cause I can’t included in the .h ?

yes that’s another problem of using that function, since you are using the object from ofApp and ofApp from the object, if you try to include ofApp.h from the object .h you’ll get a recursive include and it won’t compile. you need to use a cpp and only include ofApp.h there

1 Like

Problem solved!

Opted for the simplest mode, ofGetAppPtr.

A little bit struggle to separate all class files, there are a lot. Strings coming from vectors, feeding dynamic menus, floats and GPS data spreading points to a virtual 3D city map, all data loaded from a csv database file. Beautiful to see all working action. Great OF ! Love this C++ clambering.

I’ll be sending link as soon as I have an online reel.

Thank you all.