If you are copying that list around (i.e. passing in the list by value) then the overhead of copying a list of pointers is the least of your concerns - it probably won't even work as you expect. Each Screen will have its own separate list, and modifications won't propagate to other screens. You would need to share the same list between all screens. There are two simple ways to solve this:
You could make the list static inside the Screen
class so that only one instance will be created and shared by all Screen
s.
Or you could pass the list itself as a reference or a pointer so that all Screen
's have access to the same list and no copying is performed. I.e:
// Inside the Screen class you would store the screen as a...
// Pointer
std::list<Screen*>* _screens;
// or
// Reference, must assign in constructor/initialization list
std::list<Screen*>& _screens;
Careful with scope too. You must keep the list alive throughout the duration of the application.
But still, I'd really recommend creating a ScreenManager
class and I see no benefit from doing the list handling like this. Even if it's just a simple wrapper, it's going to be easier to understand (and extend if needed).
And by the way, a ScreenManager
class is not necessarily just a wrapper around a list. There can be many different features added to it. For example, my ScreenManager is also responsible for:
- Keeping a dictionary/map registry of screens and their ids which allows me to do everything by id:
screenManager.PushScreen("PauseScreen")
- Handling "smooth" transitions between screens, such as fades or slides. Basically, each screen has a fade out and fade in time and a float fade value between 0 and 1. The screen manager takes care of handling these transitions and interpolating the fade value. The screen decides what to do with this value (i.e. darken the screen, offset sprites, etc).
And I've seen screen managers supporting other operations such as:
- Add delays between switch/push/pop screen operations.
- Popping several screens at once.
I think it's neat to have all of this encapsulated in its own class, rather than polluting the Screen class. So, it depends a bit on what you need, but I still think creating a simple ScreenManager class would be better than passing that list around.