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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Two methods to create dialog

Two methods to create dialog

Scheduled Pinned Locked Moved C / C++ / MFC
question
6 Posts 6 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.
  • A Offline
    A Offline
    acerunner316
    wrote on last edited by
    #1

    Method 1:

    CMyDialog mMyDialog;
    mMyDialog.Create(CMyDialog::IDD, this);

    Method 2:

    CMyDialog* mptrMyDialog;
    mptrMyDialog = new CMyDialog;
    mptrMyDialog -> Create(CMyDialog::IDD, this);

    In method 2, I have to call delete mptrMyDialog before closing, but in method 1, mMyDialog will be destroyed when its parent is closed, correct? So why is method 2 generally the preferred way? Method 1 seems to me like the simpler way because you don't have to worry about deallocating.

    D M T R R 5 Replies Last reply
    0
    • A acerunner316

      Method 1:

      CMyDialog mMyDialog;
      mMyDialog.Create(CMyDialog::IDD, this);

      Method 2:

      CMyDialog* mptrMyDialog;
      mptrMyDialog = new CMyDialog;
      mptrMyDialog -> Create(CMyDialog::IDD, this);

      In method 2, I have to call delete mptrMyDialog before closing, but in method 1, mMyDialog will be destroyed when its parent is closed, correct? So why is method 2 generally the preferred way? Method 1 seems to me like the simpler way because you don't have to worry about deallocating.

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      acerunner316 wrote:

      So why is method 2 generally the preferred way?

      It all depends on the individual (and where they may have copied their code from).


      "A good athlete is the result of a good and worthy opponent." - David Crow

      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

      1 Reply Last reply
      0
      • A acerunner316

        Method 1:

        CMyDialog mMyDialog;
        mMyDialog.Create(CMyDialog::IDD, this);

        Method 2:

        CMyDialog* mptrMyDialog;
        mptrMyDialog = new CMyDialog;
        mptrMyDialog -> Create(CMyDialog::IDD, this);

        In method 2, I have to call delete mptrMyDialog before closing, but in method 1, mMyDialog will be destroyed when its parent is closed, correct? So why is method 2 generally the preferred way? Method 1 seems to me like the simpler way because you don't have to worry about deallocating.

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #3

        acerunner316 wrote:

        in method 1, mMyDialog will be destroyed when its parent is closed, correct?

        The windows object (HWND) will be destroyed when the parent is destroyed. The CMyDialog object is destroyed when it goes out of scope.  If it goes out of scope while its wrapped HWND is valid, the Windows object (HWND) will be destroyed as well. I suppose which method you use (in addition to DavidCrow's reply) depends on the desired scope of the object and/or whether you want it on the stack or the heap. Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        1 Reply Last reply
        0
        • A acerunner316

          Method 1:

          CMyDialog mMyDialog;
          mMyDialog.Create(CMyDialog::IDD, this);

          Method 2:

          CMyDialog* mptrMyDialog;
          mptrMyDialog = new CMyDialog;
          mptrMyDialog -> Create(CMyDialog::IDD, this);

          In method 2, I have to call delete mptrMyDialog before closing, but in method 1, mMyDialog will be destroyed when its parent is closed, correct? So why is method 2 generally the preferred way? Method 1 seems to me like the simpler way because you don't have to worry about deallocating.

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #4

          acerunner316 wrote:

          In method 2, I have to call delete mptrMyDialog before closing

          wrong. after !!! otherwise, you delete a dialog still being displayed !


          [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

          1 Reply Last reply
          0
          • A acerunner316

            Method 1:

            CMyDialog mMyDialog;
            mMyDialog.Create(CMyDialog::IDD, this);

            Method 2:

            CMyDialog* mptrMyDialog;
            mptrMyDialog = new CMyDialog;
            mptrMyDialog -> Create(CMyDialog::IDD, this);

            In method 2, I have to call delete mptrMyDialog before closing, but in method 1, mMyDialog will be destroyed when its parent is closed, correct? So why is method 2 generally the preferred way? Method 1 seems to me like the simpler way because you don't have to worry about deallocating.

            R Offline
            R Offline
            Russell
            wrote on last edited by
            #5

            acerunner316 wrote:

            So why is method 2 generally the preferred way?

            I never heared this before:doh:... the 2 methods are exactly the same for me. The only difference that I can see is that using pointers you can pass the pointer from function to function, or, in a class, you can store the dialog pointer on a member variable of that class.:^)


            Russell

            1 Reply Last reply
            0
            • A acerunner316

              Method 1:

              CMyDialog mMyDialog;
              mMyDialog.Create(CMyDialog::IDD, this);

              Method 2:

              CMyDialog* mptrMyDialog;
              mptrMyDialog = new CMyDialog;
              mptrMyDialog -> Create(CMyDialog::IDD, this);

              In method 2, I have to call delete mptrMyDialog before closing, but in method 1, mMyDialog will be destroyed when its parent is closed, correct? So why is method 2 generally the preferred way? Method 1 seems to me like the simpler way because you don't have to worry about deallocating.

              R Offline
              R Offline
              Roger Broomfield
              wrote on last edited by
              #6

              method 2 is preferred because the purpose of Create is to create a modeless dialog so imagine you have this function somewhere void OpenMyModelessDialog() { CMyDialog mMyDialog; mMyDialog.Create(CMyDialog::IDD, this); } as soon as the function returns the modeless dialog will be destroyed OOPS!!!

              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