Sign in to follow this  
FoxHunter2

Share variable(s) across classes

Recommended Posts

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:
CMeshX* pMeshPlayer = new CMeshX(pD3DDevice);
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

Share this post


Link to post
Share on other sites
You could try:


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 once
ThingyThatNeedsDX::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]

Share this post


Link to post
Share on other sites
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.h
IDirect3DDevice9* 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 file
extern 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

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