Pass by pointer not working
-
Hi all, I have a variable declared:
CMyDlg* m_pDlg1;
I initialized it as NULL and expected it to change when passed to CallDlg. But it stays as NULL before and after the call, therefore creating new modeless CMyDlg everytime it gets called (instead of bringing up the existing one the foreground). What did I do wrong? Shouldn't m_pDlg1 point to the newly created dialog? Please help. Thanks in advance.
void CMyApp::ActivateDlg1()
{
CallDlg(1, m_pDlg1); // the variable stays as NULL
}void CMyApp::CallDlg(int nType, CMyDlg* pDlg)
{
if(pDlg) pDlg->SetForegroundWindow(); // This never gets called
else
{
pDlg = new CMyDlg(nType);
if(!::IsWindow(pDlg->GetSafeHwnd())) pDlg->Create(IDD_MY_DLG, NULL);
}
} -
Hi all, I have a variable declared:
CMyDlg* m_pDlg1;
I initialized it as NULL and expected it to change when passed to CallDlg. But it stays as NULL before and after the call, therefore creating new modeless CMyDlg everytime it gets called (instead of bringing up the existing one the foreground). What did I do wrong? Shouldn't m_pDlg1 point to the newly created dialog? Please help. Thanks in advance.
void CMyApp::ActivateDlg1()
{
CallDlg(1, m_pDlg1); // the variable stays as NULL
}void CMyApp::CallDlg(int nType, CMyDlg* pDlg)
{
if(pDlg) pDlg->SetForegroundWindow(); // This never gets called
else
{
pDlg = new CMyDlg(nType);
if(!::IsWindow(pDlg->GetSafeHwnd())) pDlg->Create(IDD_MY_DLG, NULL);
}
}Joe Smith IX wrote:
I initialized it as NULL and expected it to change when passed to CallDlg. But it stays as NULL before and after the call, therefore creating new modeless CMyDlg everytime it gets called (instead of bringing up the existing one the foreground).
1. I do not see where, how and when you set it to NULL. 2. Did you debug your code to see the "real" values of all the variables? 3. To make debugging easier for yourself you should avoid write the code in the manner
if(pDlg) pDlg->SetForegroundWindow();
but
if(pDlg)
pDlg->SetForegroundWindow();4. how and where do you delete the m_pDlg1 pointer?
-
Hi all, I have a variable declared:
CMyDlg* m_pDlg1;
I initialized it as NULL and expected it to change when passed to CallDlg. But it stays as NULL before and after the call, therefore creating new modeless CMyDlg everytime it gets called (instead of bringing up the existing one the foreground). What did I do wrong? Shouldn't m_pDlg1 point to the newly created dialog? Please help. Thanks in advance.
void CMyApp::ActivateDlg1()
{
CallDlg(1, m_pDlg1); // the variable stays as NULL
}void CMyApp::CallDlg(int nType, CMyDlg* pDlg)
{
if(pDlg) pDlg->SetForegroundWindow(); // This never gets called
else
{
pDlg = new CMyDlg(nType);
if(!::IsWindow(pDlg->GetSafeHwnd())) pDlg->Create(IDD_MY_DLG, NULL);
}
}If you want to change the passed pointer value then you have to pass its address (because, like any other kind of parameter, pointers are passed by value) ending up with a double pointer or a reference to the pointer. e.g.
void CMyApp::CallDlg(int nType, CMyDlg & * pDlg)
void CMyApp::CallDlg(int nType, CMyDlg * & pDlg)
{
//...
} -
If you want to change the passed pointer value then you have to pass its address (because, like any other kind of parameter, pointers are passed by value) ending up with a double pointer or a reference to the pointer. e.g.
void CMyApp::CallDlg(int nType, CMyDlg & * pDlg)
void CMyApp::CallDlg(int nType, CMyDlg * & pDlg)
{
//...
}Ah, you are right. To pass by address, I need the & character. It's working fine now. Thanks so much.
void CMyApp::CallDlg(int nType, CMyDlg* &pDlg)
{
// ...
} -
Joe Smith IX wrote:
I initialized it as NULL and expected it to change when passed to CallDlg. But it stays as NULL before and after the call, therefore creating new modeless CMyDlg everytime it gets called (instead of bringing up the existing one the foreground).
1. I do not see where, how and when you set it to NULL. 2. Did you debug your code to see the "real" values of all the variables? 3. To make debugging easier for yourself you should avoid write the code in the manner
if(pDlg) pDlg->SetForegroundWindow();
but
if(pDlg)
pDlg->SetForegroundWindow();4. how and where do you delete the m_pDlg1 pointer?
1. I didn't copy the whole code, just the snippet. 2. Yes, I did. That's how I could state 'it stays as NULL before and after the call'. 3. I do. Thanks. 4. Same reason as number 1 above. Anyway, I solved it as pointed by CPallini below. I was missing the ampersand symbol to indicate passing by pointer. Thanks anyway.
-
Ah, you are right. To pass by address, I need the & character. It's working fine now. Thanks so much.
void CMyApp::CallDlg(int nType, CMyDlg* &pDlg)
{
// ...
}