SFML community forums

Help => Graphics => Topic started by: alext94 on August 05, 2013, 12:52:05 am

Title: function that returns a 2d array of sprites
Post by: alext94 on August 05, 2013, 12:52:05 am
how could you return an array of sprites ??
Title: Re: function that returns a 2d array of sprites
Post by: G. on August 05, 2013, 02:20:28 am
Like any array.
Title: Re: function that returns a 2d array of sprites
Post by: eXpl0it3r on August 05, 2013, 03:15:24 am
Or better use a std::vector or any other STL container, that fits your needs! ;)
Title: Re: function that returns a 2d array of sprites
Post by: Ixrec on August 05, 2013, 11:21:05 am
Just in case the question is about basic C++ syntax, here's exactly how you'd write it:

std::vector<std::vector<sf::Sprite> > foo() {
  //do stuff here
}
 
The space between the two >s may or may not be necessary depending on your compiler.
Title: Re: function that returns a 2d array of sprites
Post by: eXpl0it3r on August 05, 2013, 12:16:08 pm
here's exactly how you'd write it:
You should use the code=cpp tag when posting code. ;)

Anyways I don't particularly advise to use multi-dimensional vectors.
You should rather use just on vector and then use the indexing to "define" your two dimensions.
Title: Re: function that returns a 2d array of sprites
Post by: Lee R on August 11, 2013, 01:08:04 am
Like any array.

It's hard to imagine why you thought that to be a sufficient answer to the OP's questions, given that an array cannot appear as the return type of a function in C++.
Title: Re: function that returns a 2d array of sprites
Post by: Nexus on August 11, 2013, 01:23:04 pm
It's hard to imagine why you thought that to be a sufficient answer to the OP's questions, given that an array cannot appear as the return type of a function in C++.
That's one of the reasons why you should replace all arrays with std::array instances ;)
Title: Re: function that returns a 2d array of sprites
Post by: Gobbles on August 12, 2013, 02:40:06 am
That's one of the reasons why you should replace all arrays with std::array instances ;)

replace arrays with arrays?! you speak madness!
Title: AW: Re: function that returns a 2d array of sprites
Post by: eXpl0it3r on August 12, 2013, 08:29:50 am
It's hard to imagine why you thought that to be a sufficient answer to the OP's questions, given that an array cannot appear as the return type of a function in C++.
Then you might still have the same wrong understanding as the OP. Of course you can return an array:
int* foo() {
// ...
return my_areay;
}
The problem is that the size information is "lost". Thus you should bw either using a dynamic "array", aka vector, or use std::areay, which is a light C++11 wrapper, that stores the size as well.
Title: Re: function that returns a 2d array of sprites
Post by: Lee R on August 12, 2013, 08:58:18 am
I can assure you that I have no such misunderstanding. You seem to have completely ignored my carefully chosen wording. There is a difference between the address of an array in memory, and an array type (i.e. T[N]). An array T[N] /cannot/ appear as the return type of a function, thus the post I responded to was not /sufficient/ to answer the OP's question.