Thread: Big class and pointer to that.

  1. #1

    Big class and pointer to that.

    As my program is getting larger, my class is getting larger too, this is what i now have:
    class GlobalDef
    	//global variables:
    	//General DirectX8 variables
    	struct GeneralDX
    		IDirect3D8				*d3d8;
    		IDirect3DDevice8		*device8;
    		D3DDISPLAYMODE			dmode;
    		D3DPRESENT_PARAMETERS	e_present;
    		D3DCAPS8				Caps;
    		D3DDEVTYPE				rastermode;
    		DWORD					vertexmode;
    		D3DVIEWPORT8			viewport;
    		float					aspect;
    		//tris/sec specific.
    	//Time and FPS
    	struct TimeAndFPS
    		INT64 nFreq;
    		INT64 lTickCount;
    		INT64 cTickCount;
    		int mFrameRate;
    		int mFrameCount;
    		bool mInited;
    		//timeelapsed related
    		float timeStart;
    		float lastTime;
    		UINT64 tickspsec;
    	struct Matrices	
    		D3DXMATRIX				matWorld;
    		D3DXMATRIX				matView;
    		D3DXMATRIX				matProj;
    	struct Misc
    		RenderTechApplication	EnApp;
    		HWCaps					dcaps;
    		//zbuffer struct
    		ZModes					zd;
    		DWORD					ClearColor;
    		RECT					winRect;
    		bool					worldcull;
    		bool					ShowErrors;
    		char*					errstr;
    	struct Objects
    		//Text related goes here
    		vector<FontMan>		Fonts;
    		UINT				fontnum;
    		RECT				textRect;
    	//window structure
    	struct WindowStruct
    		HWND			hWnd;
    		HINSTANCE		hInst;
    		int				iWidth;
    		int				iHeight;
    		char*			clsName;
    	struct Input
    		LPDIRECTINPUT8			input8;
    		char					KeyBuf[256];
    		//mouse state
    		DIMOUSESTATE2			MState;
    		//used to check if devices are acquired.
    		bool kaq;
    		bool maq;
    There is a pointer to that class GlobalDef* g;

    My question now is, how much overhead there is? Could it be better to use global variables instead of this huge class, which will grow twice as that in the future?

  2. #2
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Dunno what you're worring about? It aint big at all.

    Just declare a static instance of your class. In this case, your class acts as nothing more than a namespace for your global variables.

  3. #3
    I shouldnt declare a pointer of it? What would be the difference between a pointer to class and just an instance to it? This class will not (possibly not..) have any functions, only variables.

  4. #4
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    > I shouldnt declare a pointer of it?

    Let me ask, would you do this:

    int* a = new int;

    or just:

    int a;

    I suppose the only thing you need to be careful of is the actual size of your class. I notice that many of the data items in it are pointers to other data (hence there are pointers of just 4 bytes).

    Do a sizeof on your class. If we are talking about KB's or more make an instance on the heap (as opposed to the stack) by calling new. Then use a static pointer to point to that instance.


    static GlobalDef* def = new GlobalDef

    Otherwise, just declare a static instance in a header file and stop worrying. Like so:

    static GlobalDef def;
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

  5. #5
    Ok, thank you very much
    I was worrying because the class will get very huge.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Replies: 25
    Last Post: 10-29-2007, 04:08 PM
  4. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM