Pass reference to constructor from within class

There’s a thread here that sort of has the same question but I just can’t crack it. I’m trying to access the methods of testApp from a object that’s been instantiated “within” it. I think my AS background is what’s killing me here. I know it’s got to be something like this but I don’t know what I’m doing wrong in the constructor/headers.

http://forum.openframeworks.cc/t/object-accessing-a-variable-outside-its-scope/628/0

The last post has an example of how to use the “this” keyword to pass a pointer or reference of the current class when instantiating a new class, and I keep having error problems with the constructor declaration and constructor itself (if I’m even making sense.)

ofClientManager.h:

  
#ifndef _OF_CLIENT_MANAGER  
#define _OF_CLIENT_MANAGER  
  
#include "ofMain.h"  
#include "ofxOsc.h"  
#include "testApp.h"  
  
class ofClientManager {  
	  
public:  
  
testApp *_testAppPointer;  
ofClientManager(testApp *_testAppPointer);  
  
// methods  
void update();  
void draw();  
  
};  
#endif  

In the above code, I’m unlcear whether I need to include testApp.h so that the “testApp *testAppPointer” points to a valid type? Right now it doesn’t seem to work, I’m sure I’m just missing some basic concept here.

[attachment=0:m6fjbeoi]code_snip.png[/attachment:m6fjbeoi]

ofClientManager.cpp:

  
#include "ofClientManager.h"  
#include "ofxOsc.h"  
  
  
testApp * testAppPointer;  
  
ofClientManager::ofClientManager(testApp *_testAppPointer)  
{  
	testAppPointer = _testAppPointer;  
	printf("\ntestApp var accessed from clientManager:%i", testAppPointer->var);  
}  
  

And in the one below, I’m not too clear on when testApp has been invoked, I know that ofMain invokes it via “new”…but there doesn’t seem to be a default constructor for testApp? In other words I’m assuming this is wrong because testApp isn’t a type yet : (testApp *_testAppPointer)

testApp.h:

  
#ifndef _TEST_APP  
#define _TEST_APP  
  
  
#include "ofMain.h"  
#include "ofClientManager.h"  
  
//--------------------------------------------------------  
class testApp : public ofBaseApp{  
  
	public:  
  
		void setup();  
		void update();  
		void draw();  
  
private:  
  
	  
		ofClientManager clientManager(testApp *_testAppPointer);  
  
};  
  
#endif  

I feel like I’m close to getting this working… I’d really be glad if anyone can give a little help! :slight_smile:

I think I get it, give this a try

in testApp.h delete this line:

  
  
 ofClientManager clientManager(testApp *_testAppPointer);  

and use this:

  
 ofClientManager clientManager;  

now in testApp::setup (in testApp.cpp) create an instance of ofClientManager like this

  
clientManager = ofClientManager(this);  

EDIT:

Is is as simple as making the argument type something like “ofBaseApp” since it’s a pointer to the type that testApp is? I feel like I’m overcomplicating this. If there’s no way to leave the type generic I assume that’s just a C++ constraint.


Thanks for the help! That makes sense…but I think this still leaves me with a problem of how to declare/implement the constructor for ofClientManager.

Ok now that I’m looking at what I posted before it makes no sense…sorry, I’ll rework it below.

What I really want to do is create a class ofClientManager that can be instantiated by “any” other class, and so I don’t want to have to define that class type in my constructor. As far as I understand, a class constructor’s argument needs to have a specific type, and in this case if the type is “someObject” (or in this case testApp) it will complain because it won’t know what someObject (testApp) is. I’m sure this is a very basic misuse/misunderstanding on my part.

ofClientManager.h:

  
  
#ifndef _OF_CLIENT_MANAGER  
#define _OF_CLIENT_MANAGER  
  
#include "ofMain.h"  
class ofClientManager {  
	  
	public:  
	  
		// methods  
		void update();  
		void draw();  
	  
		// constructor  
		ofClientManager(someObject*_someObjectPointer);  
};  

ofClientManager.cpp:

  
#include "ofClientManager.h"  
  
ofClientManager::ofClientManager(someObject* _someObjectPointer)  
{  
	someObjectPointer = _someObjectPointer;  
	cout << "The pointer to the object where this class is instantiated: << someObjectPointer;  
}  
  
void ofClientManager::update(){}  
void ofClientManager::draw(){}  

Lots of times I do this sort of thing by making a really simple base class like:

  
  
public class hasUserType {  
public:  
int userType;  
int getUserType() { return userType; }  
  

and then extending it where I need to get specific Run time type information (if you’re interested, searching for “RTTI in C++” will open up the wide world of C++ fanatics to you).

In your case, it’s probably best to create a pointer to the ofClientManager in your testApp and pass in whatever you want there. If you really want *anything* to initialize your class then you can just do void* parent, but I’m not sure how much good that would be to you.

I think that’s more or less what I’m clumsily trying to do, and in the process I think I’m starting to get a glimpse of why I’m thinking about OOP the wrong way.

Sorry if this is a stupid question, but how do I create a pointer to ofClientManager in testApp and pass things into it? What I’m currently trying to do is the following:

  
void testApp::setup(){  
		  
	clientManager = ofClientManager(this);  
	  
}  

but then I get hung up trying to set a type for this “typeless” parameter in the declarations and constructor? I assume that I’ll need that reference to testApp in the constructor, rather than passing it in from testApp in setup(). For instance, if I want to create a method to forward events from ofClientManager to testApp:

  
void ofClientManager::receiveMouseDownEvent(int x, int y){  
		  
	referenceToTestApp.MouseDown(x, y);  
	  
}  

Because this doesn’t appear to be simple to do without using or some kind of RTTI method, I suspect I have some basic lack of understanding; that this might just be the wrong way to do what I’m trying to accomplish. It feels like it should be easier, like extending testApp to get access to its methods…but that doesn’t feel right either. Basically I just want to be able to use ofClientManager as a “plug-in” solution to a few different apps, (but I assume it will always be instantiated in testApp anyway…)

Thanks again for helping a beginner.

No problem, I have an Actionscript background as well. I can’t address trying to create a plug-in for multiple apps but hopefully this can keep you moving forward

to create a pointer of ofClientManager

in testApp.h

  
ofClientManager *clientManager;  

in testApp.cpp

  
void testApp::setup()  
{  
  clientManager = new ofClientManager();  
}  

Now that clientManager is a pointer dot syntax won’t work. You have to access properties/functions like this

  
void testApp::update()  
{  
    clientManager->someProperty = someValue;  
  
    clientManager->doSomething();  
}  

This should work to get your x, y back up to testApp. 0 means the left button of the mouse

  
void ofClientManager::receiveMouseDownEvent(int x, int y){  
     ofNotifyMousePressed(x, y, 0);  
}  

Man, that helps a ton. I got it working! :smiley:

I also just noticed an OF method called ofGetAppPtr(). this seems like it might be a possibility as well, I haven’t got it to compile this way yet but it seems like it should work. The one strange quirk I noticed is that I need to include the testApp.h header in the .cpp of ofClientManager.cpp for it to recognize the type of the pointer…makes me nervous because I’m still a little unsure about how things precompile etc.

  
  
#include "ofClientManager.h"  
#include "testApp.h"  
  
ofClientManager::ofClientManager()  
{  
}  
  
void ofClientManager::setup(){  
  
((testApp*) ofGetAppPtr())->methodinTestApp();  
  
}  
  

cool - yeah, ofClientManager will need to include testApp.h to know what it is. You can put this in

ofClientManager.h since ofClientManager.cpp is including ofClientManager.h

just to pipe in here: I found these two websites to be of great help when learning C++:
http://www.learncpp.com/
http://www.cplusplus.com/doc/tutorial/

also, http://stackoverflow.com/ is a great repository of specific/special knowledge, but you may know that already. :slight_smile:

Thanks, yes indeed I’m all over those resources, they are very helpful. I’m sort of learning on the job so in this process sometimes I’m jumping a little ahead of what I know, hope I’m not asking too many basic questions…thanks again

EDIT:

I realize this is probably obvious to a lot of you but it really helped me understand the way includes and headers work if anyone is interested. I think a large part of the problem I was having was not forward declaring the ofClientManager class in testApp.h:

  
#ifndef _TEST_APP  
#define _TEST_APP  
  
class ofClientManager;  
  
#include "ofMain.h"  
#include "ofClientManager.h"  

This way testApp.h can be included in ofClientManager.h. which is needed to enable the testApp type to be known when I use the app pointer in ofClientManager.cpp:

  
((testApp*) ofGetAppPtr())   

I dreamed of the Ouroboros last night :wink: