Jump to content

View more

Image of the Day

Inventory ! Va falloir trouver une autre couleur pour le cadre D: #AzTroScreenshot #screenshotsaturday https://t.co/PvxhGL7cOH
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

C++ & SFML?

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
23 replies to this topic

#21 wintertime   Members   

4071
Like
0Likes
Like

Posted 16 October 2013 - 11:19 AM

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.



#22 Dragonsoulj   Members   

3180
Like
0Likes
Like

Posted 16 October 2013 - 08:31 PM

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;


#23 Satharis   Members   

2441
Like
0Likes
Like

Posted 17 October 2013 - 02:56 AM

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.

#24 boogyman19946   Members   

1484
Like
0Likes
Like

Posted 17 October 2013 - 01:56 PM

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.






Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.