Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: function that returns a 2d array of sprites  (Read 4142 times)

0 Members and 1 Guest are viewing this topic.

alext94

  • Newbie
  • *
  • Posts: 16
    • View Profile
function that returns a 2d array of sprites
« on: August 05, 2013, 12:52:05 am »
how could you return an array of sprites ??

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: function that returns a 2d array of sprites
« Reply #1 on: August 05, 2013, 02:20:28 am »
Like any array.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10828
    • View Profile
    • development blog
    • Email
Re: function that returns a 2d array of sprites
« Reply #2 on: August 05, 2013, 03:15:24 am »
Or better use a std::vector or any other STL container, that fits your needs! ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: function that returns a 2d array of sprites
« Reply #3 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.
« Last Edit: August 05, 2013, 12:50:26 pm by Ixrec »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10828
    • View Profile
    • development blog
    • Email
Re: function that returns a 2d array of sprites
« Reply #4 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Lee R

  • Jr. Member
  • **
  • Posts: 86
    • View Profile
Re: function that returns a 2d array of sprites
« Reply #5 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++.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: function that returns a 2d array of sprites
« Reply #6 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 ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Gobbles

  • Full Member
  • ***
  • Posts: 132
    • View Profile
    • Email
Re: function that returns a 2d array of sprites
« Reply #7 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!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10828
    • View Profile
    • development blog
    • Email
AW: Re: function that returns a 2d array of sprites
« Reply #8 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Lee R

  • Jr. Member
  • **
  • Posts: 86
    • View Profile
Re: function that returns a 2d array of sprites
« Reply #9 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.