CMeshX* pMeshPlayer = new CMeshX(pD3DDevice);
Share variable(s) across classes
Hi!
I'm currently developing my own Direct3D framework and encounter a problem each time I create a new class:
I just added a CMeshX class to load and render X-Files. This class needs a pointer to a IDirect3DDevice9* variable. Hence this variable will be necessary in various classes, I'd like to know what's the best way to share one or more variables across several classes.
Right now I'm doing this:
where pD3DDevice is the IDirect3DDevice9*, which has been created in my CD3DBase class. But I'd like all classes to be automatically able to access several most important variables without me having to pass them in the constructor or likeways.
regards
Basically, you can't. One option is to store the data in a common place and then pass references to them in the constructor, but you are already doing that.
You could try:
Of course you could make this a base class common to all things that would need the reference so they can all share it.
[Edited by - MagicScript on July 7, 2004 10:08:10 PM]
class ThingyThatNeedsDX{public: static void SetReference(IDirect3DDevice9* a) { a_= a; }protected:static IDirect3DDevice9* a_;};//Now you can cause a_ as a reference to the device//once SetReference is called onceThingyThatNeedsDX::SetReference(blah);
Of course you could make this a base class common to all things that would need the reference so they can all share it.
[Edited by - MagicScript on July 7, 2004 10:08:10 PM]
If you want to access that through inheritance you had better make that protected instead of private ;)
Quote:Original post by Kibble
If you want to access that through inheritance you had better make that protected instead of private ;)
Right; the inheritence was sort of an afterthought.
Quote:But I'd like all classes to be automatically able to access several most important variables without me having to pass them in the constructor or likeways
this is probably completely wrong . . . . but it sounds like you're trying to have multiple variables available outside all of these classes. one way to do that is to create a header file with these constants such as
// Constants.hIDirect3DDevice9* g_device;// ect// and then, in [separate file] whichever class header file you // need, before the #define, type :#include "Constants.h"// the extern tells the compiler that the actual variable// is in another header fileextern IDirect3DDevice* g_var#ifndef _RANDOM_HEADER_H#define _RANDOM_HEADER_H// ect
hope that sort of helps. probably bad syntax, can't remember if you're supposed to leave the #defines off the constants header file. reference "extern" and see. just remember to initialize all the constants/variables before you use them - but i didn't need to tell you that
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement