Sign in to follow this  
rmetzger

global pointer for direct3d device?

Recommended Posts

In a book i've been reading, they create an Engine class that has a direct3d device. The Engine class also has an extern global Engine pointer. This way the the direct3d device doesn't have to be passed into every class that needs it as long as the Engine.h header file is included in the class. Is this a good thing to do or should I just pass the device into classes as needed? What are the pros and cons to this? Thanks

Share this post


Link to post
Share on other sites
It is preferable, from a design sense, to pass the object to whatever interfaces require it. This makes dependencies explicit and aids in modularization/isolation, which in turn improves reusability, maintainability, and testability of code. It forces you to make a conscious choice about where and when you pass the dependency (the D3D device) and whether it is actually correct and necessary to do so, or if the underlying design you are following is flawed.

The book is likely using a global approach because either (a) it is a bad book, or (b) it is not a book that is aimed to be about design issues, but rather a demonstration of how to use D3D. The latter is slightly more forgivable. The method of using a global pointer is valid and will function, but is suboptimal for scale.

Share this post


Link to post
Share on other sites

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