We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › I need to use a pointer
Page Index Toggle Pages: 1
I need to use a pointer (Read 354 times)
I need to use a pointer
Jul 15th, 2008, 7:38pm
 
Here's the problem, simplified...

I've got two boxes. I call them A and B. Both are objects of the class "Module" which has a number of sub-classes.

There's also a list menu. When something on the menu is pressed the selection of A or B is changed. Here's some Pseudo Code:
if(top is selected){
 selected = A
 switch to the bottom
}else{
 selected = B
 switch to the top
}

The trouble is this part.
Code:
selected = A 


I don't usually use pointers, but for this scenario I have to. Otherwise, I have to copy everything that can happen for A and B in to two lists with different left-sides because the next part right now looks like this Code:

switch(menu_index){
case 0: selected = new SubOne(a,c,b,d,e,f,g);
case 1: selected = new SubTwo(h,i,j,k,l,m);
case 2: selected = new SubOne(n,o,p,q,r,s,t);
case 3: selected = new SubThree(u,v,w,x,y,z);


and so on... Can I please make a pointer? Help?? This sketch is already enormous. I can't afford to make two lists of all these menu commands.
Re: I need to use a pointer
Reply #1 - Jul 15th, 2008, 7:47pm
 
Java doesn't have pointers, everything is a reference.

I'm not quite sure what you're trying to do.. but I believe you can just do"selected = A"

e.g.
Code:
class foo{
int x;
foo(int y){
x=y;
}
}

foo A,B;

void setup()
{
A=new foo(1);
B=new foo(2);
}

void draw()
{
foo C;
if(thing)
C=A;
else
C=B;
}


This doesn't copy A to C, it just says that C is a reference to the same object as A is.

Re: I need to use a pointer
Reply #2 - Jul 15th, 2008, 8:10pm
 
Thanks for the speedy reply! It got me back on the right track. I think I understand what's happening.

I can do C=A or C=B...
but the functions of the menu can't use Code:
C = new Subfoo(d,g,e,g,d) 

because C is its own object.

Instead, I think I'll have to put the two items (though there are only two) in to an array. Then, I can attack the global variable but in a different way. It's like.. make-shift memory addresses.
Re: I need to use a pointer
Reply #3 - Jul 15th, 2008, 9:08pm
 
I think you can just do it in reverse...

e.g.
Code:

thing C;

switch(something)
{
case 1: C=foo1(...); break;
case 2: C=foo2(...); break;
}

if(top is selected)
A=C;
else
B=C;
Page Index Toggle Pages: 1