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():

	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);  
	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)  

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.


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.


in testApp.h:

class testApp : public ofSimpleApp{  
   int testAppVar;  

in testApp.cpp:

void testApp::setup(){  

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(){  
  b=new Ball();  
void draw(){  
class 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  
		static AGlobalVideoImageData* Instance();  
		IplImage *origSizeFrameCopy;  

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

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


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.



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


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!


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{  
		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;   
// Singleton.h  
#ifndef _SINGLETON		  
#define _SINGLETON		  
#include "ofMain.h"  
// Declaration  
class Singleton {  
    static Singleton* Instance(); // pointer to itself  
	int testVal;   
    Singleton(); // protected constuctor  
    static Singleton* _instance;  
// 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:

   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.