Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Pass by pointer not working

Pass by pointer not working

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Joe Smith IX
    wrote on last edited by
    #1

    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);
    }
    }

    V CPalliniC 2 Replies Last reply
    0
    • J Joe Smith IX

      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);
      }
      }

      V Offline
      V Offline
      Victor Nijegorodov
      wrote on last edited by
      #2

      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?

      J 1 Reply Last reply
      0
      • J Joe Smith IX

        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);
        }
        }

        CPalliniC Online
        CPalliniC Online
        CPallini
        wrote on last edited by
        #3

        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)
        {
        //...
        }

        In testa che avete, signor di Ceprano?

        J 1 Reply Last reply
        0
        • CPalliniC CPallini

          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)
          {
          //...
          }

          J Offline
          J Offline
          Joe Smith IX
          wrote on last edited by
          #4

          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)
          {
          // ...
          }

          CPalliniC 1 Reply Last reply
          0
          • V Victor Nijegorodov

            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?

            J Offline
            J Offline
            Joe Smith IX
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • J Joe Smith IX

              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)
              {
              // ...
              }

              CPalliniC Online
              CPalliniC Online
              CPallini
              wrote on last edited by
              #6

              I gave you a flawed code sample, sorry. :-O Thank you for pointing it out so nicely.

              In testa che avete, signor di Ceprano?

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups