Object accessing a variable outside its scope

Ciao all!

I’m following the wiki entry about OOP and I got it working, the problems raise when I want to access a variable OUTSIDE the scope of the object.

For example this code in testApp setup():

  
n=1;  
	balls= new ofBall*[n];  
	for(int i=0;i<n;i++){  
		balls[i]=new ofBall(ofRandom(0,ofGetWidth()),ofRandom(0,ofGetHeight()),ofRandom(1,100));  
	}  

and this code in Ball constructor:

  
r = _r;  
	  
	x = _x;        
	y = _y;  
	vx = ofRandom(-1,1);             
	vy = ofRandom(-1,1);  
	  
	int tempGrayValue=10+ofRandom(0,245);  
	red=green=blue=tempGrayValue;  
	  
	printf("\ntestApp var accessed from ball:%i",n);  

I receive this as a result:
error: ‘n’ was not declared in this scope

i don’t really know/understand what you want to accomplish, but i try to guess.
please try to provide the signature and scopes of your(member-) functions,
so we can help you more easily.

i guess, first you want to dynamically allocate an array ofBall-objects
and set the values. an easy approach would be

  
  
void somefunc  
{  
  //...  
  
  int n = 1;  
  ofBall* balls = new ofBall[n];  //now you have n instances of ofBall and n calls to the default constructor  
  //should check/be prepared for out-of-memory-situations  
  
  for (int i = 0; i < n; ++i)  
  {  
     balls[i]->SetValues(ofRandom(0,ofGetWidth()),ofRandom(0,ofGetHeight()),ofRandom(1,100));  
  }  
  
  //...  
}  
  

you don’t need a default constructor because the compiler will generate one,
but it is good practice since only then you can initialize all values.
then implement a “SetValues” function so you can set the values afterwards.

i don’t know why want to access “n” in ofBall (ctor or whereever) - it doesn’t make sense. why should a ball object know that it is the 1st, 2nd, 3rd, … ball?
if you want to have unique ids, then try static counters and an id member variable.
and of course the compiler screams. n is not in the scope of the constructor.

*hth*

Sorry, yesterday was a tough one and my question was not clear enough.

Basically I want to access a public variable in TestApp from inside an object.

example:

in testApp.h:

  
  
  
class testApp : public ofSimpleApp{  
	  
   public:  
  
   int testAppVar;  
  
}  
  
  

in testApp.cpp:

  
void testApp::setup(){  
  
   testAppVar=12345;  
  
}  

in the Ball constructor:

  
printf("\n testAppVar var accessed from inside ball:%i",testAppVar);  

I’m trying this because in Processing is always possible to access a public variable in PApplet from an object instantiated inside the PApplet setup(), while in OF it seems to be a bit different.

in Processing I would do this way:

  
  
int PAppletVar;  
  
Ball b;  
  
void setup(){  
  
   PAppletVar=12345;  
    
  b=new Ball();  
  
}  
  
void draw(){  
  
}  
  
class Ball{  
  
   Ball(){  
  
    println("PApplet var accessed from inside Ball:"+PAppletVar);  
  
   }  
  
}  

getting this as a result:
_
PApplet var accessed from inside Ball:12345_

I tend to put things I need to access from several objects into a singleton and then have the objects get an instance of that to read vars from, possibly not the best solution, but it works and I use it all the time.

It goes something like:

  
  
// AGlobalVideoImageData.h  
  
class AGlobalVideoImageData  
{  
	public:  
	  
		static AGlobalVideoImageData* Instance();  
		~AGlobalVideoImageData();  
  
		IplImage *origSizeFrameCopy;  
  
	private:  
  
		AGlobalVideoImageData();		  
	  
};  

  
  
// AGlobalVideoImageData.cpp  
  
#include "AGlobalVideoImageData.h"  
	  
AGlobalVideoImageData* AGlobalVideoImageData::Instance()  
{  
	static AGlobalVideoImageData inst;  
	return &inst;  
}  
  
AGlobalVideoImageData::AGlobalVideoImageData()  
{  
	origSizeFrameCopy = 0;  
}  
  
AGlobalVideoImageData::~AGlobalVideoImageData()  
{  
  
}  

And you get an instance to it like this:

  
globalVideoData = AGlobalVideoImageData::Instance();  
  

sure they work. i use them from time to time and sometimes they come in really handy. global vars also do…
BUT be aware of the :evil: & :twisted: !!!

hey nice!
now I know how a singleton looks like in c++ :slight_smile:

thanks for the code

Anytime!

I’m pretty sure I got the gist of that code from www.gamedev.net in the first place, was trying to find it so I could link to it to provide credit, context and I’m sure there would be a discussion on the site to go with it as well, but wasn’t able to find it.

Will edit this post if I remember when it came from.

/A

claudio,

I think the thing about processing is that it compiled classes as inner classes. Some info here:

http://www.developer.com/java/article.php/899361

inner classes do have access to their parent classes functions and methods, where as pure classes don’t. To do pure classes in p5, when you add a tab, use .java not .pde as the extension.

hope that hels!
zach

hi,

i went through the example with “AGlobalVideoImageData” from above but as a total beginner i had some troubles, so after a while i figured it out, but i thought it might be helpfull to post this example with “where to put what” as well:

  
  
// testApp.cpp  
//-------------------------------------  
  
#include "testApp.h"  
  
void testApp::setup(){	   
	//Singleton *testinst = Singleton::Instance();   
	testinst = Singleton::Instance();   
	std::cout << "testvalue-----------------------(" << testinst->testVal << ")\n\n";  
	testinst->testVal =100;  
	std::cout << "testvalue-----------------------(" << testinst->testVal << ")\n\n";  
}  
  
  
// testApp.h  
//-------------------------------------  
  
#ifndef _TEST_APP  
#define _TEST_APP  
  
#include "ofMain.h"  
#include "Singleton.h"  
  
  
class testApp : public ofSimpleApp{  
	  
	public:  
		  
		void setup();  
		void update();  
		void draw();  
		  
		void keyPressed(int key);  
		void keyReleased(int key);  
		void mouseMoved(int x, int y );  
		void mouseDragged(int x, int y, int button);  
		void mousePressed(int x, int y, int button);  
		void mouseReleased();  
  
		Singleton *testinst;   
  
};  
  
#endif	  
  
  
// Singleton.h  
//-------------------------------------  
  
#ifndef _SINGLETON		  
#define _SINGLETON		  
  
#include "ofMain.h"  
  
// Declaration  
class Singleton {  
public:   
    static Singleton* Instance(); // pointer to itself  
	  
	int testVal;   
	  
protected:   
    Singleton(); // protected constuctor  
private:  
    static Singleton* _instance;  
};  
  
  
#endif   
  
  
// Singleton.cpp  
//-------------------------------------  
  
#include "Singleton.h"  
   
// Implementation   
Singleton* Singleton::_instance = NULL;  
  
Singleton* Singleton::Instance() {  
		  
	// checks if instance already exists:  
	if (_instance == NULL) {  
        _instance = new Singleton;  
    }  
	return _instance;  
	  
	// from OF forum:  
	/*  
	 static Singleton inst;  
	 return &inst;   
	 */  
}  
  
Singleton::Singleton() // constructor  
{  
	testVal = 20;  
  
}   
  
  

everytime i need to access a variable that lives inside some object i create a pointer to that object and use it as a reference. and when inside a class and want to pass that as a reference you can use the comand “this”. so for example in your code you could do it like this:

  
  
n=1;  
   balls= new ofBall*[n];  
   for(int i=0;i<n;i++){  
      balls[i]=new ofBall(ofRandom(0,ofGetWidth()),ofRandom(0,ofGetHeight()),ofRandom(1,100), this); //"this" returns a pointer to the testApp object  
   }  
  

and in your ball constructor

  
  
  
testApp * testAppPointer;  
ofBall::ofBall(/*your other variables...*/, testApp *_testAppPointer){  
  
//your other variables...  
  
testAppPointer = _testAppPointer;  
 printf("\ntestApp var accessed from ball:%i", testAppPointer->n);  
}  
  
  

this was all from the top of my head but hope it works.

rui