Extending an extension ofBaseApp...

I’m a bit new at ofx/C++, but not at programming, in general (very experienced with C#, Java, VB, others). I’ve created my first real application, which is a lightweight, super fast blob tracker and it works great. My goal is to create an alternate base application that has blob tracking/calibration/etc built-in.

The app is called “Magnetic”, and I have a class called “ofxMagneticApp” in my “addons” folder. “ofxMagneticApp” extends “ofBaseApp”. When I extend testApp from ofxMagneticApp, it works fine. However, if define setup(), it crashes (crash is reportedly related to openCV). I’m using virtual void to define public methods (“ofxMagneticApp.h” has no “cpp” counterpart).

Works:

  
#ifndef _MAGNETIC  
#define _MAGNETIC  
  
#include "ofMain.h"  
#include "ofxMagneticApp.h"  
  
class Magnetic : public ofxMagneticApp  
{};  
  
#endif  

Doesn’t work (setup is defined in “Magnetic.cpp”)

  
#ifndef _MAGNETIC  
#define _MAGNETIC  
  
#include "ofMain.h"  
#include "ofxMagneticApp.h"  
  
class Magnetic : public ofxMagneticApp  
{  
  
public:  
  
    // Override routines  
    void setup();  
  
};  
  
#endif  

Any ideas?

If I’m reading this right ofxMagneticApp.h looks like this?

  
ofxMagneticApp : public ofBaseApp {  
virtual void foo(){} // no reference to setup or any other baseapp methods  
}  

That should be fine, so I wondering if the problem is in ofxMagneticApp or in your new app that you’re creating? Can you post the actual error that you’re getting?

[quote author=“joshuajnoble”]If I’m reading this right ofxMagneticApp.h looks like this?

  
ofxMagneticApp : public ofBaseApp {  
virtual void foo(){} // no reference to setup or any other baseapp methods  
}  

That should be fine, so I wondering if the problem is in ofxMagneticApp or in your new app that you’re creating? Can you post the actual error that you’re getting?[/quote]
I do have references to setup and base app stuff, and that is most likely the real problem. The errors reported seem to be byproducts.

Best way around that?

I ended out just calling those function “mSetup”, “mDraw”, etc and call them from their counterparts in testApp.cpp in my example project. Not as elegant as I’d like, but I really can’t spend any more time on it.

Just following up on this…

In C#, I can create a virtual function in my base class like so…

  
  
public BaseClass  
{  
  
      private int counter = 0;  
  
      public BaseClass()  
      {  
  
            setup();  
  
      }  
  
      private virtual void setup()  
      {  
  
            counter++;  
            // counter is now 1  
  
      }  
  
}  
  

When I go to extend it, I can override that function AND call it, thus executing the contents of both the base class and the extension…

  
  
public MyClass : BaseClass  
{  
  
      public MyClass()  
      {  
  
            setup();  
  
      }  
  
      private override void setup()  
      {  
  
            counter++;  
            base.setup(); // Runs function in base class  
  
            // counter is now 2  
  
      }  
  
}  
  

Is there an equivalent in C++?

Here’s one way to do that, note the virtual declaration:

  
  
class testExtendsApp : public ofBaseApp {  
public:  
	virtual void setup() { printf(" OK from testExtendsApp "); }  
};  
  
  

now I can overwrite that in the child implementation

  
class testApp : public testExtendsApp {  
	  
	public:  
		  
		void setup();  
  
};  
  

and in my testApp.cpp I call the parent by using the scoping operator, class::method()

  
  
void testApp::setup() {	   
	// call super  
	printf(" OK from testApp ");  
	testExtendsApp::setup();  
}  
  

There’s other ways to do this, and maybe even better ones, but for reading up on method overriding in cpp http://www.cs.bu.edu/teaching/cpp/inheritance/intro/ or http://stackoverflow.com/questions/429125/override-and-overload-in-cpp might help.

Ah, that makes sense. Thanks much!!!

That worked. Much cleaner, phew… Yeah, this is much better than base.myVirtualVoid() since you can specify which base class you’re calling, in the event you extend multiple classes with the same functions. Coooool.

Life is better now.