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

Author Topic: Set a vector in another class and returning...is that ok a good idea?  (Read 1316 times)

0 Members and 1 Guest are viewing this topic.

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Hello there people,

At the moment I have a simple std::vector<Object*> objects
Object is a custom class i have created which can be text, sprite, animated sprite etc etc.
Now in my GameState class the code is starting to get a little large and I was wondering If I can do this.

The objects variable is a vector of pointers to "Object"s. Is it safe for me to do the following.

Create a new class called "StateLoader"
StateLoader will then deal with setting up the objects for whatever the game state is in
and then return a std::vector<Object*>, is this a safe thing to do or not?

When I mean safe, if is ok to populate a vector of pointers, and then set a variable to be equal to that vector of pointers? I just want to populate my objects variable outside of my gamestate class as the class is getting abit large because of all the objects being set.

If anyone can shine some light onto this it would be great.

Regards,

Canvas

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Set a vector in another class and returning...is that ok a good idea?
« Reply #1 on: January 07, 2014, 12:40:12 am »
I strongly advise you against the use of such "can be anything" classes, it breaks the whole idea of having a strong typed language in the first place and you'll have to cast everything around, wasting a lot of resources and creating a lot of potential problems.

You also can't just assign the address of an object to the vector, because as soon as that object gets out of scope the address will point to an invalid memory space and if you used new to create space on the heap, you're doing manual memory management which is (mostly) obsolete with modern C++ - you might want to read some stuff on RAII.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: Set a vector in another class and returning...is that ok a good idea?
« Reply #2 on: January 07, 2014, 08:57:49 pm »
what is a good way to load resources and store them in sfml then?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
AW: Set a vector in another class and returning...is that ok a good idea?
« Reply #3 on: January 08, 2014, 12:41:35 am »
You can use STL containers, but use one container for each type and for resources such as textures use an unique_ptr.
You could take a look at the ResourceHolder from the SFML Game Development book. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/