Jump to content
  • Advertisement
Sign in to follow this  
The Najdorf

two c++ questions

This topic is 4621 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

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.

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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 ;)

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!