Jump to content
  • Advertisement
Sign in to follow this  
discodowney

Returning an array in C++

This topic is 3085 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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 code
D3DXVECTOR vecs[3];

DoSomethingWithThem(vecs, 3);

// Implementation
void DoSomethingWithThem(D3DXVECTOR* vecs, int count)
{
// ...
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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; }
};

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!