# C++ Function Return Array

## Recommended Posts

SulphurTenM    122
I know this seems REALLY stupid, but I want to make a function return an array, a 2-dimensional array. Now, I know this can be done really easy in Java: -
public int [][] getArr()
{
int [][] newArr = new int[4][2] ;

return( newArr ) ;
}


But it's a little more complex than this... The array I want returned is part of a class. Example: -
//header file

{
private:
int arr[4][2] ;
public:
int [][] getArr() ; //this is wrong
} ;

#endif


//implementation file

{
for( int i = 0 ; i < 4 ; i++ )
{
arr[i][0] = i ;
arr[i][1] = i+1 ;
}
}

int [][] HeaderFile::getArr() //this is wrong
{
return( arr ) ;
}


Somewhere in another class or the main file: -
//...

int arr[4][2] ;

arr = hf.getArr() ; //this is the way I want it
//...


How do I do this? I don't want to have to use vectors or anything like that... Templates and pointers also.... why can this not be as simple as in Java? (Don't answer that) How do I get the desired result? I am using slightly wrong syntax? Or do I have to completely rewrite some parts?

##### Share on other sites
rvdwerf    102
I'am sry to tell you but a function cannot return an array.
You need to return a pointer to an array instead.

##### Share on other sites
SulphurTenM    122
Urg. Ok then....
I will figure out a long winded way....

I feared this would be the case... thanks anyway...

##### Share on other sites
Nitage    1107
You'd probably be better of returning a reference to a std::vector.

##### Share on other sites
rip-off    10976
Often, get...() functions indicate poor design where object behaviour is embedded in client classes. Ask yourself: why am I returning this array? What does the array represent?

If your class is a POD type, then why not have the member public?
You could embed the array in a structure and return a const reference to that structure:
struct InnerArray {    int arr[4][2]};class HeaderFile{private:   InnerArray inner;public:    HeaderFile() ;    const InnerArray &getArr();};

##### Share on other sites
Antheus    2409
Quote:
 Now, I know this can be done really easy in Java: -

C++ equivalent of that would be:
typedef std::vector< std::vector<int> > MyArray;class Header {MyArray &getArr() {  return arr;}private:  MyArray arr;}

The arrays in Java take care of all the allocations and bounds checking - they are equivalent to std::vector, not raw arrays or pointers.

The other way is to use the struct wrapper as mentioned above. But by using arrays in such liberal manner, you're setting yourself up for problems down the line.

Quote:
 How do I do this? I don't want to have to use vectors or anything like that...Templates and pointers also.... why can this not be as simple as in Java? (Don't answer that)

But I have to, since it's the cause of your problem.

If you want Java functionality, use vectors.

BTW: what does your array represent?

##### Share on other sites
Nuno1    161
It is possible to pass the array through an object, but it seems like you are doing somthing wrong.

Why not provide a function like Get/PutArrayItem( int row, int col ) that will provide an access to the array inside your object ?

Nuno1

##### Share on other sites
Zahlman    1682
Why are you exposing this data directly to the outside world? This is coming from someone with a Java background?! What does the data represent exactly?

For that matter, why would you have a default constructor for something that evidently (from the name) needs to be read from a file?

##### Share on other sites
SulphurTenM    122
The examples I used were purely examples, the names were descriptive of what they are, not what they model...

I have "solved" it by making my own Array object and passing that around... with the suggested accessor and mutator methods....

I know it's not the perfect way... but thanks for all of your ideas.

I guess I should have realised that Java takes care of all the "messy" stuff...

I am making tetris in C++ with OpenGL/GLUT. The array contains the locations of each of the blocks of a piece.

While I am sure there are some much more efficient ways of coding this, I am doing it an OOD/OOP way. I am not going to claim that I am doing in a completely correct way. However, I have so far got all seven shapes falling down the screen and a given rate, you can "hard drop" a shape, and you can move it left and right until it hits the sides.

I have yet to get the following working: -
Rotation
Collision (everything from hitting the bottom to hitting other pieces to line completion)
Scoring
Display of next shape

However, I see little difficulty in completing these. I guess this is for a different forum....

I am doing this to prove to myself that I can.
I have made previous game-like OpenGL programs, but they are not complete.
Example of such a program.

[Edited by - SulphurTenM on November 23, 2007 7:53:25 PM]

##### Share on other sites
Dancin_Fool    785
Quote:
 Original post by SulphurTenMI guess I should have realised that something like this is beyond C++...

LOL!!! As you've been shown earlier, it's quite possible in c++ through std::vector. If you'll use an array in java, why wouldn't you use c++'s vector class?

##### Share on other sites
SulphurTenM    122
Sorry, yeah, I screwed that up a bit...
I edited that comment out...