Dynamic ofGetAppPtr()

Hello everyone!

I am trying to make a custom class call certain functions in testApp… For example, I have a function in testApp called toggleBackground and I execute it with


This works perfectly. Now I would like to make it dynamic and have the function name in a variable… something like…

var functionName;

Does anyone know how I can set the function name variable (currently using a string which I am guessing is wrong) and then call it?

Thank you so much.

Can you give more context about what you want to do. In c++ you cant call functions with strings. But what your trying to do might be accomplished with functions pointers

Hi Ahbee,

Thanks for getting back to me… I am making a simple button class…

In testApp I am calling my class successfully with

void testApp::draw(){
    homeButton.makeSimpleButton(30, 660, 200, 60, 30, 120, 189, 190, "buttonimg.png", "toggleBackground()");

See the toggleBackground() at the end? I would like to be able to make that dynamic so each instance can have it’s own callback function.

My button class:

class ofxSimpleButton{
        float buttonX;
        float buttonY;
        float buttonW;
        float buttonH;
        int buttonR;
        int buttonG;
        int buttonB;
        int buttonA;
        string callbackfunc;
        ofImage buttonImg;
    void makeSimpleButton(float _x, float _y, float _w, float _h, int _r, int _g, int _b, int _a, string imgpath, string callback){
        buttonX = _x;
        buttonY = _y;
        buttonW = _w;
        buttonH = _h;
        buttonR = _r;
        buttonG = _g;
        buttonB = _b;
        buttonA = _a;
        callbackfunc = callback;
        if(buttonR != -1.0 && imgpath == ""){
            ofSetColor(buttonR, buttonG, buttonB, buttonA);
            ofRect( _x, _y, _w, _h);
        }else if(imgpath != ""){
            //cout << "loadimage" << endl;
            buttonImg.draw(buttonX, buttonY);
    void pressSimpleButton(){
        int mousex = ((testApp*)ofGetAppPtr())->mouseX;
        int mousey = ((testApp*)ofGetAppPtr())->mouseY;
        if(mousex > buttonX && mousex < buttonX+buttonW && mousey > buttonY && mousey < buttonY+buttonH){
            cout << callbackfunc.c_str() << endl;



You can use function pointers. In newer openframeworks, testApp becomes ofApp

class ofApp;

class SimpleButton {
    void (ofApp::*callback)(void);
    void makeButton(void (ofApp::*userCallback)(void)){
        callback = userCallback;
    void pressButton(){

buttonCallback is a user defined function

void ofApp::setup(){

YES!!! This works perfectly. I have also changed testApp to ofApp. Cheers.