Question about global variables vs. passing pointers!

This topic is 3381 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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!

Share on other sites
Why do your font and input classes need to store a pointer to the device?

Share on other sites
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?

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?

Share on other sites
Quote:
 Original post by livengoodshould I [...] turn the graphics class into a singleton?

Share on other sites
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.

Share on other sites
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.

Share on other sites
Quote:
 Original post by livengoodI 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

Share on other sites
Quote:
 Original post by MaulingMonkeyI hate globals and singletons in general

I'm with you.

Share on other sites
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!

Share on other sites
Quote:
 Original post by MaulingMonkeyFull disclosure: I hate globals and singletons in general, so I'm a little biased.

every seasoned coder does....

Share on other sites
Quote:
Original post by RobTheBloke
Quote:
 Original post by MaulingMonkeyFull disclosure: I hate globals and singletons in general, so I'm a little biased.
every seasoned coder does....

The seasoned coders I know don't hate them. I look at them suspiciously, but I'm not foolish enough to discount their legitimate uses.

There are software elements which legitimately exist as singletons and as globals. Choosing to use an improper tool, or not use the right tool, based on the tool's popularity is unwise.

Share on other sites

This topic is 3381 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Create an account

Register a new account

• Forum Statistics

• Total Topics
628760
• Total Posts
2984544

• 12
• 25
• 12
• 10
• 17