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

Author Topic: Global variable vs Pass by reference  (Read 2439 times)

0 Members and 1 Guest are viewing this topic.

TehKyle

  • Newbie
  • *
  • Posts: 8
    • View Profile
Global variable vs Pass by reference
« on: May 20, 2011, 02:49:55 am »
Is it better to have RenderWindow as global variable or send it as reference to the function?

Map.Draw(&AppWindow) looks wonky and wondering if there is better way to code

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Global variable vs Pass by reference
« Reply #1 on: May 20, 2011, 04:21:45 am »
Send it as a reference.
I use the latest build of SFML2

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Global variable vs Pass by reference
« Reply #2 on: May 20, 2011, 12:43:31 pm »
Use pointers or references. Avoid global variables where possible.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

xazax

  • Guest
Global variable vs Pass by reference
« Reply #3 on: May 20, 2011, 01:17:37 pm »
What about singletons?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Global variable vs Pass by reference
« Reply #4 on: May 20, 2011, 02:06:18 pm »
Quote from: "xazax"
What about singletons?
Singletons are global variables. They share a lot of their problems, it's just that some people consider singletons a good solution because they are object-oriented. Their original intention was to ensure single instances, however they are often overused and abused as "better" global variables.

The best option is to keep dependencies as local as possible, therefore you should normally prefer passing function arguments.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: