C++ & SFML?

Started by
22 comments, last by boogyman19946 10 years, 6 months ago

That sounds like you want everything to access everything, when it would be safer to only have the higher level logic know about all those systems.

Advertisement

If you want to have a private variable that stores the window, change it to a pointer and create a window with new:

In declaration:

sf::RenderWindow * mWindow;

In constructor:

mWindow = new sf::RenderWindow(sf::VideoMode(640, 480), "SFML!");

In destructor:

delete mWindow;

It's not exactly a singleton. I just need my input object, sound object, gui object etc to access each other.

Which works fine if you pass references between things or have a container object to access them, if code was "just better" to have everything public and visible to each other we wouldn't have access modifiers at all.

I think passing things around all the time is more like spaghetti code than to have 1 single place to store a reference to something. Less typing as well.

I could argue against this but frankly there's at least a couple hundred live or half-dead threads on these forums going over this topic so I'm not going to bother, needless to say most people here are going to suggest you not just make everything global, public, or a singleton just because you think it makes it easier to type/deal with.

Hint: It doesn't. Definitely not in the long term.

I was able to make it work.

It's not exactly a singleton. I just need my input object, sound object, gui object etc to access each other.

I think passing things around all the time is more like spaghetti code than to have 1 single place to store a reference to something. Less typing as well.

There is a lot of hidden dangers with using globally accessible variables. For one thing, it's hard to keep track of all the points of access. Because the objects can be accessed at any time, there is little options for the code to tell you that you need to initialize some global variable before the code can work correctly. At some point you'll forget that one of the classes in your code depends on a global variable and you'll forget to initialize it. Now you're wasting time fixing something that could have been prevented had you declared it as an input argument in your constructor. Sure, you can declare everything global, but you should keep in mind that scoping and proper class interfaces are there to tell you what information you need and hide information that should not be accessed elsewhere. It limits how many things you'll have to focus on and will keep you from creating functions that have hidden side effects.

Yo dawg, don't even trip.

This topic is closed to new replies.

Advertisement