global pointer for direct3d device?

Started by
0 comments, last by jpetrie 14 years, 2 months ago
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
Advertisement
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.

This topic is closed to new replies.

Advertisement