Hypotheticaly, if you have a large object that is needed almost everwhere within a program and this object is used for accessing data. Is it better to pass the object by referance into each function that needs it, or to make the object static?

For some context, I am most of the way through developing a game and have began to question some of my earlier engine decisions with regard to an object I called 'CentralAssetManager'.
It has only one instance and is used to load and enumerate all art assets used by the game so that each game object needs only an integer 'address' in order to referance an asset.

Recommended Answers

All 2 Replies

Pass them by reference. Its better than polluting globals.

Hypotheticaly, if you have a large object that is needed almost everwhere within a program and this object is used for accessing data. Is it better to pass the object by referance into each function that needs it, or to make the object static?

Hypothetically, I'd question whether that large object is really needed everywhere or if it's just a lazy design. There's probably a better way to organize the program where that object's scope is limited or need for the object removed entirely.

On the other hand, global variables are one of those things that are easily abused, and that's why the advice is to avoid them. But when they really do provide the best option, they should be used carefully. Obfuscating things to avoid global variables isn't a better solution in my opinion.

Your CentralAssetManager might be best implemented as a singleton. It's still global, but much more controlled than just some random variable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.