Sign in to follow this  
Marx2052

Class Pointers not so easy...

Recommended Posts

Ok this is essentially a pointer problem but that doesn't mean switch off ;) C++ and DirectX 8.1 Program. Right i've created my classes as so: Control.h CGame* pGame; CUIClass* pUIClass; Control.cpp pGame = new CGame(); pUIClass = new CUIClass(etc, etc, pGame, etc, etc); and wish to pass the a pointer to the class into a seperate class so that i can work out an iterface based on contained values, i thought since that is a pointer to a piece of memory it'd be as simple as passing that value on creation of the class i.e.: CUIClass.h CGame *pGameLoc CUIClass.cpp CUIClass(etc, etc, pGame, etc, etc); { pGameLoc = pGame; } In theory I could then use pGameLoc to retrieve mouse data etc etc from the game class. However this is not true and from my debugging it seems i'm passing an instant of the pGame class to the CUIClass i.e. the values of the pGameLoc are the values of the class when it was passed even though i know the class is working fine since i can see my mouse moving and everything else is running smoothly. Is there a problem with pointers when declareing classes using new or am i just using the wrong combination of pointers and i've testing all the combinations that make sense and some that don't. Any help would be great thanks.

Share this post


Link to post
Share on other sites
What is the actual declaration of the CUIClass constructor? What you're doing (or at least trying to do) should work perfectly fine, unless of course you really are inadvertently making a copy of CGame and your CUIClass is using the copy.

Share this post


Link to post
Share on other sites
It's created in the same way as the CGame class and in the same file, CControl.cpp.

i.e.

Control.h

CGame* pGame;
CUI* pUI;

Control.cpp

pGame = new CGame(m_pD3DDevice);
pUI = new CUI(m_pD3DDevice, pGame, &m_nScreenWidth, m_nScreenHeight);

thats the order they are created in aswell right after each other.

P.S. whats this boards code for changing text to code with a scroll bar?

Share this post


Link to post
Share on other sites
AHHH HAA!!!

I've found the problem which really i shoulda caught earlier...

pGame = new CGame(...);

was called later i'm my program by an initilisation function which would explain the problem since a new CGame was created and the pointer stored in CUI still pointed to the old CGame.

Thanks for your help you helped me look past the "ITS A POINTER ERROR!" phase.

Share this post


Link to post
Share on other sites
Quote:
Original post by Marx2052
pGame = new CGame(...);

was called later i'm my program by an initilisation function which would explain the problem since a new CGame was created and the pointer stored in CUI still pointed to the old CGame.

Thanks for your help you helped me look past the "ITS A POINTER ERROR!" phase.


It's not a pointer error. Your use of pointers was entirely correct. It's a global variable error. Global variables are bad news. You should avoid global variables if at all possible, and it's almost always possible.

It's also an aliasing error. Aliasing is severe bad news when using pointers. Avoid pointer aliasing if at all possible, and it's always possible.

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