Sign in to follow this  

two c++ questions

This topic is 4403 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
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
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
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
You can return a struct containing an array:


template<class T, int S>
struct fixed_vector
{
T data[S];
operator T* () { return data; }
T & operator[] (int i) { return data[i]; }
operator const T* () const { return data; }
const T & operator[] (int i) const { return data[i]; }
};

fixed_vector<int, 5> function ()
{
fixed_vector<int, 5> foo;
// ...
return foo;
}

fixed_vector<int, 5> foo = function();



One of the STL's container types might be a better choice, though. Can you be certain the array will always need to be the same length?

Share this post


Link to post
Share on other sites
Quote:
Original post by Nathan Baum
You can return a struct containing an array:

*** Source Snippet Removed ***

One of the STL's container types might be a better choice, though. Can you be certain the array will always need to be the same length?


I don't see any advantage in this over vector<int>(5)?

Share this post


Link to post
Share on other sites
Quote:
Original post by wild_pointer
Quote:
Original post by Nathan Baum
You can return a struct containing an array:

*** Source Snippet Removed ***

One of the STL's container types might be a better choice, though. Can you be certain the array will always need to be the same length?


I don't see any advantage in this over vector<int>(5)?

There are two potential advantages:

1. The size of a "fixed_vector<int, 5>" is known at compile-time, and so can be stack allocated.

2. Because the size of "fixed_vector<int, 5>" is part of its type, it is a compile-time error to use a 4-element fixed vector where a 5-element fixed vector is expected.

Both of these advantages are also eo ispo disadvantages, so it's very dependent upon context as to which technique is more appropriate.

Share this post


Link to post
Share on other sites
Quote:
Original post by Nathan Baum
You can return a struct containing an array:

*** Source Snippet Removed ***

One of the STL's container types might be a better choice, though. Can you be certain the array will always need to be the same length?

boost::array provides a similar functionality. I believe it is one of the libraries included in TR1.

CM

Share this post


Link to post
Share on other sites
Quote:
Original post by Conner McCloud
Quote:
Original post by Nathan Baum
You can return a struct containing an array:

*** Source Snippet Removed ***

One of the STL's container types might be a better choice, though. Can you be certain the array will always need to be the same length?

boost::array provides a similar functionality. I believe it is one of the libraries included in TR1.

CM

Not similar functionality: far superior functionality. Use that instead of rolling your own!

Share this post


Link to post
Share on other sites

This topic is 4403 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this