two c++ questions

Started by
14 comments, last by Nathan Baum 18 years, 4 months ago
1) How do I do a function that returns an array (not the pointer, the whole array): I tried something like
int function()[]
{
int foo[5];
...
return foo;
}
then calling it with
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
class Ragdoll
{
Ball balls[10];
};


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.
Got a Mac? Check out my game at [a]http://www.radicalrebound.com[/a]
Advertisement
It would be more along the lines of int[] function(). But you'd probably want to return a pointer instead... So more like int* function().

For your class issue, simple type "class Ball;" before the Ragdoll class definition. The linker will fix it all up later. What actually happens is that by putting that there you're telling the compiler that you'll have the definition later and so it puts it off for later.
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
edit: Dammit! Beaten.
Heh... but on the topic of your first question, I'm not too sure about that. Actaully returning an array. You might want to pass it as a reference in the parameters... Oh, and I'd love to beet you to post first again anytime. ;)
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
Thanks...

1) reading from google it seems that you just can't do it, which is ridiculous >:(

2) I cant try it now but wouldn't it say "duplicate class" or something?
Got a Mac? Check out my game at [a]http://www.radicalrebound.com[/a]
Don't do this:
int * func(){  int array[5];  return array;}
You'll return a pointer to memory that doesn't exist anymore (the stack frame disappears on a return statement).

Instead, do this:
int * func(){  int * array;  array = new int[5];  return array;}
The memory from this will be in the heap, which is yours to play with.
william bubel
1) Not really ridiculus. Think of the overhead on it! Just pass a pointer to it as a parameter friend!

2) Nope. You're simply stating that "Later I will have the class definition, but for now know that it will be there."
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
1)Ok thanks, I guess I'll just pass the pointer to the array from outside and modify the array inside. (I dont want to use dynamic memory if I can avoid it)

2) Fine thx
Got a Mac? Check out my game at [a]http://www.radicalrebound.com[/a]
Quote:Original post by The Najdorf
1)Ok thanks, I guess I'll just pass the pointer to the array from outside and modify the array inside. (I dont want to use dynamic memory if I can avoid it)

2) Fine thx


That's fine, but be aware that Windows will only give you around 1 MB for the stack I believe, or so I'm told by my friend at Rose Holman...
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
If this truly is C++ code, then just return an std::vector. In a release build, it's light-weight like an array.

If you DON'T want to use STL, then you're going to have to return the pointer to the array, and in one of your function arguments, "return" the size. For example, the last argument of your function might be "size_t& theArraySize" .. before you return, you just set theArraySize to the size of the array you are returning (which returns the address of the first element in the array, btw. this is in fact, a pointer).

Of course, you could actually swap this, and pass in your array as an argument and return the size. Or you could pass them both in and return an error-code ;)

This topic is closed to new replies.

Advertisement