Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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?

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?

Share this post


Link to post
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 this post


Link to post
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.

Thanks for your replies.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by RobTheBloke
Quote:
Original post by MaulingMonkey
Full 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 this post


Link to post
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.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this