Two c++ questions

Member
Posts: 749
Joined: 2003.01
Post: #1
1) How do I do a function that returns an array (not the pointer, the whole array):

I tried something like
Code:
int function()[]
{
int foo[5];
...
return foo;
}

then calling it with

Code:
int foo[5]=function();

but it doesnt seem to work.

2) Say I have a "Ragdoll" class that is composed by 10 "Balls". Now, I want every ball to have a pointer to the Ragdoll of which it's a part of.


So I would have
Code:
class Ragdoll
{
Ball balls[10];
};
Code:
class Ball
{
...
Ragdoll* ragdoll;
};

But the compiler does not like this, since the "Ball" class needs the ragdoll class to be defined and viceversa.

How should I do it?

Thanks.

©h€ck øut µy stuƒƒ åt ragdollsoft.com
New game in development Rubber Ninjas - Mac Games Downloads
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
1) either use std::vector, or new --

Code:
int *function() {
    int *foo = new foo[5];
    return foo;
}

Note that that makes it the caller's responsibility to delete [] the results.

2) forward-declared the Ragdoll class --

Code:
class Ragdoll;

class Ball {
    Ragdoll *owner;
};
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #3
Basically, you can't. You have to create dynamic memory and return the pointer to that. Or you can use a global array. Or a static array that you return the pointer to, but that will get overwritten, along with a global array. You could, however, pass in an already created array and have the function edit it.
Quote this message in a reply
Moderator
Posts: 365
Joined: 2002.04
Post: #4
Just to elaborate on what Keith said, you can get the exact result you're looking for with std::vector:

Code:
std::vector<Class> Function()
{
    std::vector<Class> container;
    container.push_back(Class(parameters));        // For example

    return container;
}
That would give the caller a complete copy of the vector and its contents. The downside of this approach is you're potentially invoking lots of copies. If you're copying a lot of data, or if Class has a complex copy constructor and assignment operator, that would be a bad thing. On the other hand, if you're copying ten small, simple objects occasionally it's a perfectly reasonable thing to do.

Neil Carter
Nether - Mac games and comic art
Quote this message in a reply
Member
Posts: 749
Joined: 2003.01
Post: #5
Thanks.

>You could, however, pass in an already created array and have the function edit it.

I think i'll stick to this one, thanks

©h€ck øut µy stuƒƒ åt ragdollsoft.com
New game in development Rubber Ninjas - Mac Games Downloads
Quote this message in a reply
zKing
Unregistered
 
Post: #6
Also in C/C++ pointers and arrays are (mostly) interchangable, i.e.:

Code:
int* function()
{
    int* foo = new int[5];  // create an array of 5 int's
    return foo;
}

void main()
{
    int* bar = function();
    bar[3] = 8675309;
}

WARNING: Playing with pointers like this can ruin your day if you don't know what you are doing... and sometimes even when you do.

This works because an array is just a spot in memory with the values in a contiguous row:

A line like this:
int foo[5];

Will create something like this:
| int0 | int1 | int2 | int3 | int 4 |

When you do this:
int* bar = foo;

The "bar" pointer points to the address of the first element in the foo array (int0 in this case).

Code like "bar[3]" and "foo[3]" both translate to the same thing:
Take the base address (foo or bar) and add three times 'sizeof(int)' to it.

You run into trouble if foo/bar are NULL, garbage or you go running off the end of your array... so be careful. The compiler will be quite happy to generate code for this:

int bla[10];
bla[1234] = 0;

But lord knows where that zero is going to land in memory, if you are lucky it just crashes, if not you get strange "why is my program doing that?" bugs.

For your case you could try something like this:
Code:
class Ragdoll;
class Ball
{
public:
     Ball(Ragdoll* parent) : ragdoll(parent)
    {
    }
private:
    Ragdoll* ragdoll;
};

class Ragdoll
{
    Ball* balls;
    void SetupBalls()
    {
        Ball ballsArray[] = { Ball(this), Ball(this), Ball(this), Ball(this), Ball(this),
                              Ball(this), Ball(this), Ball(this), Ball(this), Ball(this), };
        balls = ballsArray;
    }
};
Quote this message in a reply
Post Reply