Question about global variables vs. passing pointers!

Started by
9 comments, last by frob 15 years, 7 months ago
I have been making a directX framework and when I finished making the font class and the input class I noticed that I am always passing a pointer to each class so they can have an internal pointer to the d3d device. Those two classes are just examples. So my question is: Would it be best to just make the device global or should I continue to pass a pointer to the classes when I create them. Also, should I store that pointer internally in each class as a member or perhaps turn the graphics class into a singleton? These questions always seem to come up when i'm programming and don't know what is 'best'. Thanks in advance!
Advertisement
Why do your font and input classes need to store a pointer to the device?
It needs to use it when creating the font (D3DXCreateFont). I suppose this create could be done inside the graphics class with a function call to get the descriptor from the font class. Is that what you mean?

Thanks for the quick reply!

Edit: I suppose I could just pass it to the functions that need it? I am using it in multiple functions to change the font though ie: IsItalic() because I need to recreate the font. Is this the proper way to do it?
Quote:Original post by livengood
should I [...] turn the graphics class into a singleton?

Please don't. Kittens will die.
My system has a directsprite class that handles textures and provides a AddDrawOrder() so anything with a pointer to it can tell it to do work. Internally it holds the d3d device pointer.

Any of my systems that use text get a pointer to this directsprite, so they can internally dish out their draw orders. Through that pointer, they can also access the GetDevicePtr() method, which they only call during ini when the font is created.

Not sure if that helps, but seeing someone elses setup usually helps me get ideas.

And about the singleton, and the vague answers you may get about it, and or links to huge blocks of text about it, im going to try and provide some info.

You dont want a singleton, because you dont need it. Having a class that when used, sits in global scope with only one instantiation, is fine and dandy, and is what my system uses. There is no stopping having two at a time, it might not make sense to have two, but its ok. Singletons are almost only needed when you have to make sure, for certain, that only one exists. Like when programming for a handheld device with very, very limited memory.
Ya I understand about the singletons. The question I'm trying to ask isn't really to get my programming 'working' as its working just fine. I'm just curious if what I do with the pointers is the a good way to do it.

Thanks for your replies.
Quote:Original post by livengood
I have been making a directX framework and when I finished making the font class and the input class I noticed that I am always passing a pointer to each class so they can have an internal pointer to the d3d device. Those two classes are just examples.

This is fine. The advantages of this over a global/singleton are:
1) It's not hardcoded to any specific device. If, say, you ever had to support multiple devices at the same time (multi monitor support?), you'd be fine.
2) It explicitly documents your dependencies, making it easier to see when your code is getting hard to maintain by having too many of them.
3) It's easier to make it work with other code. If two libraries both create and manage their own (global/singleton) device, making them play together can be a pain in the ass.

It's a common pattern and even has it's own name: Dependency Injection. All that fancy stuff about Inversion of Control and such is basically saying #3: Your code is less brittle if you tell things what to use (e.g. by passing them to classes) instead of having them try to find things on their own (e.g. by having them access globals and singletons and what else).

Full disclosure: I hate globals and singletons in general, so I'm a little biased.



It's also possible you may only need the device temporarily inside your constructor, e.g.:

class MyFont {    LPD3DXFONT font;public:    MyFont( LPDIRECT3DDEVICE9 device ) {        HRESULT r = D3DXCreateFont( device, ..., &font );        if (!OK(r)) { ... }        ...    }};


I don't remember enough D3D to remember if the device is needed for later, but I thought it might be worth mentioning.

HTH
Quote:Original post by MaulingMonkey
I hate globals and singletons in general

I'm with you.
Great! This is almost exactly what I do. I make sure to only store a pointer if I'm going to recreate a device later or need it for something. If anyone else has any input please let me know! Thank you maulingmonkey!
Quote:Original post by MaulingMonkey
Full disclosure: I hate globals and singletons in general, so I'm a little biased.


every seasoned coder does....

This topic is closed to new replies.

Advertisement