Archived

This topic is now archived and is closed to further replies.

Accessing other classes without parsing them possible?

This topic is 5931 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Say I have two classes, the first class has only one instance at one time (for example a Direct3D Device class), the second class can have many instances (such as a texture class). Instead of parsing a pointer to the other class to access its members, is it possible to somehow grab the pointer without parsing the pointer? In other words, can I group certain classes so they can access eachover? If so, how would I make sure the other class has been created?

Share on other sites
i don''t understand what you mean by "parsing a pointer", but you can make classes friend s so they can access each other''s private members...

--- krez (krezisback@aol.com)

Share on other sites
I know the question is a bit far out, though it would save alot of re-coding in my circumstance.

A friend class can access private members, what im on about is instead of accessing the other class by parsing it, is there another way, for example:

Normally:
Class CGfx{ public:  void Function();}Class CTexture{public:  whatever();  }// For CTexture to access a functon in CGfx, // you would need to have a function that // grabs the other class:CTexture::CTexture(CGfx *GfxPointer){    GfxPointer->Function();}

Alternatively ???

CTexture::Ctexture() // Note there is no CGfx pointer{   _MagicRetriever->Function();}

Or can I somehow group certain classes?

Share on other sites
If your CGfx class is only used once you could declare everything in it static.

Or you could have a CGfx::CreateTexture(CTexture*); or something like that. This way cgfx handles the creation and can set a member of ctexture to "this".

Also I think you have the word parsing and passing mixed up. It should be passing a pointer.

Share on other sites
Hello,

I have a couple of sugestions. First if it doesnt matter else where from where you have to call one of the classes, you can just nest one class inside the other. This would only be useful if it didnt matter if you had to "double access" a class with CTex->CDXDriver->something.

Second idea, you can create the class CDXDriver, then create the CTex class passing the address of CDXDriver, eg:

class CTex
{
private:
CDXDriver *DXD;
public:
CTex(CDXDriver *DXDriver):DXD(DXDriver){}
};

Well, I hope atleast this gave you some ideas. If you have any questions or comment, feel free to email me. Daniel Holley
Daniel.holley@usa.net

Share on other sites
Ok, thanks, thats given me some ideas.

Now one more question, how do you make sure a pointer to a class has not been deleted?
Is there a CheckClass(CMyClass) function or something similar?

Im guessing I could save the first 16 bytes of data at the pointer location when the class pointer is saved, then re-check the data bytes when I need to use the pointer.

But if I do this when the class has been deleted, would it produce an illegal memory address error? If a class gets deleted, its memory is freed up, so another class may have taken its posision in memory.?

Edited by - Anarchi on February 24, 2002 10:04:58 PM

Share on other sites
Anarchi: i think i get it now... you have a single instance of the CGfx class, and you want to use it within your CTexture class, right?
you can pass a pointer to CGfx when you instanciate your CTexture class (i.e. pass it to the constructor, or have a "bindGfx()" function):
// keep a CGfx pointer as a member in the CTexture class// in this example, it is called m_CGfxPtrCTexture::CTexture(CGfx* DaGfxClass)  {  m_CGfxPtr = DaGfxClass;  };// orCTexture::bindGfx(CGfx* DaGfxClass)  {  m_CGfxPtr = DaGfxClass;  };// then, to use it:CTexture::DoStuff(void)  {  m_CGfxPtr->Function();  };

it doesn''t really solve your problem, since you still have to pass it the pointer, but at least you only have to do it once.
you could make the CGfx object public, but a lot of people would call that bad style.

Share on other sites
its quite infeasible to access something when you dont know where it is.

basically you will want to use a static pointer.

  // PSUDEO CODECTexture{private: static CGfx *mGfx;public: static SetGFX(CGfx *newGfx); blah();}// now somewahere in your init code you do// this can be done before you create any CTexturesCTexture::SetGFX(gfx_ptr);

you can spice this up by calling SetGFX() from within the constructor of a CGfx object assuming you will have only one and want them all to be assoiciated to that one, then you can have the destructor set the value to null.

ALWAYS delete the textures and such BEFORE deleting the gfx class, because you must always delete things that have dependencies first (ie in reverse order of creation). so you should NEVER run into a point in time where the CTexture class will not have access to a valid CGfx class (since in dx textures MUST be destroyed before the device anyway), but if you want when you delete the CGfx class you can call CTexture::SetGFX(NULL) and put an
if(!mGfx)
return ERR;

check which will then fail on any call when the CGfx is not valid. again though this should NEVER be allowed to happen because all apis dictate that you destroy things in reverse order, and such would require deletion of the textures first then the device.

btw with this method (ie using static) you only have to do this ONCE at the start after you create the CGfx object. all the CTextures will "magically" have access to the pointer value via that member varible.

Edited by - a person on February 24, 2002 12:36:19 AM

Share on other sites
Hey,
All you have to do is add a function in the class that the pointer points to that is something like this:
bool ValidClass(){return true;}
When you need to check to see if it is still there just:
if(pDXDriver->ValidClass()){...}
Agian this is just one way of doing it. Daniel Holley (Jag_007)

Share on other sites
yeah thats right Jag, and have the program crash or worse do something dangerously stupid. calling functions from an object that may not exist is VERY bad, just like accessing memory that dont belong except you are calling code and could possible do VERY VERY bad things.

• 9
• 23
• 10
• 19