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 › return reference of a class in a function
Page Index Toggle Pages: 1
return reference of a class in a function (Read 365 times)
return reference of a class in a function
Nov 28th, 2008, 11:01pm
 
Maybe someone answer this before, but I didn't find it. I don't find how to return reference of a class in a function.

I was trying somethings with a small example to reduce complexity and find how to do it.

Code:

class image
{
int width;
int height;
}

image getImageSize(int x, int y)
{
image temp = new image();
temp.x = x;
temp.y = y;
return temp;
}



Error display is:
expecting SEMI, found 'getImageSize'
Re: return reference of a class in a function
Reply #1 - Nov 28th, 2008, 11:54pm
 
The main culprit is that when you define a function, you have to define setup() as well.

Other issues:
- You have width and height fields, but set x and y...
- You shouldn't use 'image' as class name, as you can see from syntax highlighting, it is a keyword (it is the name of a function in processing).
To avoid such clash, tradition is to name classes with an initial uppercase letter. Here, I would use ImageSize, for example.
Re: return reference of a class in a function
Reply #2 - Nov 28th, 2008, 11:57pm
 
Try this.
image i;

void setup(){
i = function(10, 10);
}

void draw(){
i = function(11,11);
}
image function(int x, int y){
 image temp = new image();
 temp.x = x;
 temp.y = y;
 return temp;
}

class image {
 int x, y;
}


I would suggest avoiding the word image: http://processing.org/reference/image_.html

Also, you are using "global" variables "width" and "height".  I'd avoid that as well, making it more specific to your need, or adhering to some idiom, like adding a trailing underscore to indicate that this is a local or class variable.

Update: oh, PhiLho got to it first.

Re: return reference of a class in a function
Reply #3 - Nov 29th, 2008, 11:41am
 
Thanks to both. The code "sw01" works perfectly and it is just what I was trying to do.

I didn't see this sintax in any place before now and I think is sometimes usefull.

Page Index Toggle Pages: 1