Returning an array in C++

Started by
7 comments, last by discodowney 14 years ago
No idea how to do it. The same way in java doesnt work. i tried a way i found on google but that didnt seem to work either. basically i have an array of D3DXVECTOR3 that i compute for a bounding box. Now im tryin to get them values from a getBounds() method. Anyone help me out here?
Advertisement
Have the caller keep an array of D3DXVECTOR:s and pass them by pointer or reference to the function, along with the number of elements in the array:

// In calling codeD3DXVECTOR vecs[3];DoSomethingWithThem(vecs, 3);// Implementationvoid DoSomethingWithThem(D3DXVECTOR* vecs, int count){  // ...}
think you misunderstood. I have a BoundBox class. the bounding box is calculated and stored in bounds - D3DXVECTOR3 bounds[8];

What im trying to do is draw the box so i can be sure its in the right position around my mesh. but i cant do that from in the bounding box class.

so i have to pass bounds back to my class spaceShip. So the BoundingBox is an attribute of this class. So i need to do somehting like

boundingbox = boundingBox.getBounds();

where get bounds returns bounds up above.
You can't return an array. At best you can return a reference to one :)

struct X{    int array[8];    int (&get_array())[8] { return array; }};int main(){    X x;    int (&arr)[8] = x.get_array();}


But it is probably a lot simpler to return a pointer to the beginning of the array.

struct X{    int array[8];    int* get_array() { return array; }};
Grand that worked.
But how do i go up through the elements now?

D3DXVECTOR3 boundBox = boundingBox.getBounds()

thats bounds[0]. but how do i refer to bounds[1]

What you can do is pass a value by reference to get the amount of indexes you have and return the first element of the bounding box

int counter=0;
D3DXVECTOR3* Bounds = getBounds(&counter);

where counter holds the number of bounds.
class BoundingBox
{
public:

D3DXVECTOR3* GetBounds(){return bounds;}

private:

D3DXVECTOR3 bounds[8];

};

Then when you want to access it:
BoundingBox blabla;
D3DXVECTOR3* myBounds=blabla.GetBounds();
myBounds[0]...
myBounds[1]..
etc...
Obviously you'll get an error if you write myBounds[8], so be carefull, this isn't the most "correct way", but it will work
Quote:Original post by discodowney
Grand that worked.
But how do i go up through the elements now?

D3DXVECTOR3 boundBox = boundingBox.getBounds()

thats bounds[0]. but how do i refer to bounds[1]


Actually, since you are returning a pointer, it would be:
D3DXVECTOR3* boundBox = boundingBox.getBounds()

Then you can address the ith element one by going:
boundBox

Of course, doing it this way, you don't know the length of the array. Using BornToCode's suggestion of encapsulating length and array into a struct and returning the struct works. Or you can return the length of the array by passing in a reference.

int size
D3DXVECTOR3* boundBox = boundingBox.getBounds(&size)

and getBounds returns size in the passed in pointer.
Its always gonna be 8 so im grand there.

Okay. thanks for the help.

This topic is closed to new replies.

Advertisement