Quantcast
Channel: GameDev.net
Viewing all articles
Browse latest Browse all 17825

When/How often should I be using forward declaration?

$
0
0
So I recently ran into a problem where my Game class needed to know about my GraphicDevice class (Its being used as member variable) and my GraphicsDevice class needed to know about my Game class (Its being used as a method param) //In my Game.h file #include <Windows.h> #include "GraphicsDevice.h" class Game { public: /*Other methods and things*/ GraphicsDevice graphicsDevice; }; //In my GraphicsDevice.h file #include <d3d11.h> #include "Game.h" class GraphicsDevice { public: /*Other stuff*/ void init(Game* game); }; Now I know this wont compile because it causes a circular dependency issue and that it can be easily be solved with forward declaration But it started to make me wonder, should I be forward declaring everything? Not only classes but structs too? EG: //In GraphicsDevice.h //Structs from d3d11.h (directx 11 header) //Not directly used in the header file, but needed to be known for the member variables struct IDXGISwapChain; struct ID3D11Device; struct ID3D11DeviceContext; struct ID3D11RenderTargetView; class GraphicsDevice { public: /* Other methods and stuff */ IDXGISwapChain* swapchain; ID3D11Device* device; ID3D11DeviceContext* deviceContext; ID3D11RenderTargetView* backBuffer; }; Normally I just include the header file, but I'm not sure if this is correct. Should I be making DTO like class instead and the use that for the properties that I need to pass from Game to GraphicsDevice?

Viewing all articles
Browse latest Browse all 17825

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>