That's a fairly common way. It makes the most sense if the values are somehow related in such a way that there is some logical name for the class which contains them. For example, if you want to return the x, y, z coordinates of a point in space, it makes sense to define a class:
<code><pre>class 3DCoordinate {
double x;
double y;
double z;
}</pre></code>
...especially if you anticipate needing to use those variables together in other situations.
You can also use pre-existing container objects such as arrays and Vectors. Your original func(int& x, float* y) example can be mimicked by:
<code><pre> void func(int[] xHolder, float[] yHolder) {
xHolder[0] = 1000;
yHolder[0] = 1.22f;
}</pre></code>
Calling this is a little strange:
<code><pre> int[] X = new int[1];
float[] Y = new float[1];
func(X, Y);
int x = X[0];
float y = Y[0];
</pre></code>
That's just one of many possibilities though - most others are strange too. The best bet is probably to go with the first idea, and extend it by grouping the method func()
with the data it operates on, as part of the same class:
<code><pre>class ValueFinder {
public int x;
public float y;
public void func() {
x = 1000;
y = 1.22f;
}
}</pre></code>
Then you get the values later from outside this class like so:
<code><pre> ValueFinder vf = new ValueFinder();
vf.func();
int x = vf.x;
float y = vf.y;
</pre></code>
A more complete example:
<code><pre>class Calculator {
private int a; // an input
private int b; // an input
private int c; // an input
private int x; // an output
private int y; // an output
public void setA(int a) { this.a = a; }
public void setB(int b) { this.b = b; }
public void setC(int c) { this.c = c; }
public int getX() { return x; }
public int getY() { return y; }
public void calculate() {
x = 3 * a + b % 7;
y = a * c - 14;
}
}
public class
Test {
public static void main(
String[]) {
Calculator calc = new Calculator();
calc.setA(635);
calc.setB(571);
calc.setC(549);
calc.calculate();
int x = calc.getX();
int y = calc.getY();
}
}</pre></code>
This is a fairly typical Java idiom. It may seem like a lot of extra code overhead compared to C/C++, but if the values a, b, c, x, and y are actually related and are used together with any frequency, it can be fairly useful and readable.
Though it's nowhere near as elegant-looking as Perl's lists, or Python's tuples, IMO. But we do what we can with what we have.