Skip to main content
gamedev.net
🔒 Locked

Wrong Pointer Values

Started by MannyZanny Dec 31, 2003 at 11:42 AM 6 replies 400+ views
Original Post
MannyZanny
MannyZanny
I have a Class1 with a variable called Test which is initialized at 0. In another class (Class2) a pointer is used to retreive the Test value. The problem is it will always get the value 0 even after Class1 changes the variable to 1. My pointer looks like this. Class1 *pointer = new Class1; if (pointer->Test == 0) { MessageBox("0"); } else if (pointer->Test == 1) { MessageBox("1"); } delete pointer; I''m sorry for putting this up because there are articles on pointers all over but I jus can''t figure it out. I also tried: Class1 pointer; if (pointer.Test == 0) { MessageBox("0"); } else if (pointer.Test == 1) { MessageBox("1"); }
SiCrane
SiCrane
If all you do with the Class1 object is create it, it never has a chance to change the variable to 1, you''d need to at least manipulate the object for it''s member variables to change from their initialized state.
fizban75
fizban75
More code please, specifically what you are doing in Class2 and the code that changes the Test value to 1 in Class1. You current example doesn''t show the problem sufficiently for anyone to answer it.
MannyZanny
MannyZanny
Here is the actual code:

Childframe creates the variables and changes the values when a toolbar button is pressed.

CChildFrame::CChildFrame()
{
// TODO: add member initialization code here
m_MoveView = 0;
m_ZoomView = 0;
m_RotateView = 0;
}

void CChildFrame::OnViewMove()
{
// TODO: Add your command handler code here
m_MoveView = 1;
m_ZoomView = 0;
m_RotateView = 0;
}

void CChildFrame::OnViewRotate()
{
// TODO: Add your command handler code here
m_MoveView = 0;
m_ZoomView = 0;
m_RotateView = 1;
}

void CChildFrame::OnViewZoom()
{
// TODO: Add your command handler code here
m_MoveView = 0;
m_ZoomView = 1;
m_RotateView = 0;
}

In another CView class. When the left mouse button is pressed. The value is shown.

void CTop::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CString Test;

CChildFrame *pointer = new CChildFrame;

Test.Format("%1i", pointer->m_MoveView);

delete pointer;

MessageBox(Test);

COpenGL::OnLButtonDown(nFlags, point);
}
Megahertz
Megahertz
Looks to me your just creating an instance of the CChildFrame class, which calls the default constructor, thusly setting the m_MoveView member to 0.

After that returns you get that value and store it in a string, which indeed should be 0. (You never modified it anywhere!)

Finally you delete the instance of the class which destroys any information that class may have contained.

On the next mouse press, the process just repeats itself.

Not totally sure what your trying to do, but it seems you''re writing an editor of somekind and wanna change the actions that occur when the user clicks and drags the mouse in one of the 4 view windows. (Just a guess =) )

-=[ Megahertz ]=-




-=[Megahertz]=-
MannyZanny
MannyZanny
Thats Right.

I''m Just having trouble getting the state of the toolbar buttons.
fizban75
fizban75
Is there another instance of CChildFrame somewhere that is actually receiving events? Because the instance of that class that you''re inspecting is not.
MannyZanny
MannyZanny
This is the whole class

CChildFrame::CChildFrame()
{
// TODO: add member initialization code here
m_bInitSplitter = FALSE;

m_MoveView = 0;
m_ZoomView = 0;
m_RotateView = 0;
}

CChildFrame::~CChildFrame()
{
}

BOOL CChildFrame:reCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs

if( !CMDIChildWnd:reCreateWindow(cs) )
return FALSE;

return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CChildFrame diagnostics

#ifdef _DEBUG
void CChildFrame::AssertValid() const
{
CMDIChildWnd::AssertValid();
}

void CChildFrame::Dump(CDumpContext& dc) const
{
CMDIChildWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CChildFrame message handlers

BOOL CChildFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
// CG: The following block was added by the Splitter Bar component.
CRect Rect;

GetClientRect(&Rect);

m_wndSplitter.CreateStatic( this, 2, 2 );

m_wndSplitter.CreateView( 0, 0, RUNTIME_CLASS(CTop),
CSize(Rect.Width()/2, Rect.Height()/2), pContext );

m_wndSplitter.CreateView( 0, 1, RUNTIME_CLASS(CPerspective),
CSize(Rect.Width()/2, Rect.Height()/2), pContext );

m_wndSplitter.CreateView( 1, 0, RUNTIME_CLASS(CFront),
CSize(Rect.Width()/2, Rect.Height()/2), pContext );

m_wndSplitter.CreateView( 1, 1, RUNTIME_CLASS(CSide),
CSize(Rect.Width()/2, Rect.Height()/2), pContext );

m_bInitSplitter = TRUE;

return TRUE;
}

void CChildFrame::OnSize(UINT nType, int cx, int cy)
{
CMDIChildWnd::OnSize(nType, cx, cy);

// TODO: Add your message handler code here
CRect Rect;

GetClientRect(&Rect);

if (m_bInitSplitter && nType != SIZE_MINIMIZED )
{
m_wndSplitter.SetColumnInfo(0,Rect.Width()/2,10);
m_wndSplitter.SetColumnInfo(1,Rect.Width()/2,10);
m_wndSplitter.SetRowInfo(0,Rect.Height()/2 - 5,10);
m_wndSplitter.SetRowInfo(1,Rect.Height()/2 + 5,10);

m_wndSplitter.RecalcLayout();
}
}

void CChildFrame::OnViewMove()
{
// TODO: Add your command handler code here
m_MoveView = 1;
m_ZoomView = 0;
m_RotateView = 0;
}

void CChildFrame::OnViewRotate()
{
// TODO: Add your command handler code here
m_MoveView = 0;
m_ZoomView = 0;
m_RotateView = 1;
}

void CChildFrame::OnViewZoom()
{
// TODO: Add your command handler code here
m_MoveView = 0;
m_ZoomView = 1;
m_RotateView = 0;
}

void CChildFrame::OnUpdateViewMove(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_MoveView);
}

void CChildFrame::OnUpdateViewRotate(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_RotateView);
}

void CChildFrame::OnUpdateViewZoom(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_ZoomView);
}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.