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. dynamic_cast fails

dynamic_cast fails

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
3 Posts 2 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.
  • M Offline
    M Offline
    Maxwell Chen
    wrote on last edited by
    #1

    There is an Edit control on the dialog. The ID is IDC_EDIT. In CMyDialog::OnInitDialog(), I do this ...

    CEdit* pEdit = dynamic_cast<CEdit*>(GetDlgItem(IDC_EDIT) /* = some valid address */);
    if(!pEdit) {
    // Error;
    }

    The CWnd* returned from GetDlgItem failed to convert to a CEdit*. But isn't CEdit derived from CWnd? :confused:

    Maxwell Chen

    L 1 Reply Last reply
    0
    • M Maxwell Chen

      There is an Edit control on the dialog. The ID is IDC_EDIT. In CMyDialog::OnInitDialog(), I do this ...

      CEdit* pEdit = dynamic_cast<CEdit*>(GetDlgItem(IDC_EDIT) /* = some valid address */);
      if(!pEdit) {
      // Error;
      }

      The CWnd* returned from GetDlgItem failed to convert to a CEdit*. But isn't CEdit derived from CWnd? :confused:

      Maxwell Chen

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Hi Maxwell,

      Maxwell Chen wrote:

      But isn't CEdit derived from CWnd

      Yes, but GetDlgItem does not always return a CEdit pointer so the dynamic_cast runtime check fails. Have a look at the following document: Inside MFC: Handle Maps and Temporary Objects[^] Essentially... when GetDlgItem cannot find a permanent object in the MFC CHandleMap it will return a pointer to a CTempWnd object. So your code fails to dynamic_cast from CTempWnd to CEdit. As an experiment you should add a CEdit member variable to your class for IDC_EDIT. This will cause the CHandleMap to have a permanent handle. Now your code will magically work. :) Best Wishes, -David Delaune

      M 1 Reply Last reply
      0
      • L Lost User

        Hi Maxwell,

        Maxwell Chen wrote:

        But isn't CEdit derived from CWnd

        Yes, but GetDlgItem does not always return a CEdit pointer so the dynamic_cast runtime check fails. Have a look at the following document: Inside MFC: Handle Maps and Temporary Objects[^] Essentially... when GetDlgItem cannot find a permanent object in the MFC CHandleMap it will return a pointer to a CTempWnd object. So your code fails to dynamic_cast from CTempWnd to CEdit. As an experiment you should add a CEdit member variable to your class for IDC_EDIT. This will cause the CHandleMap to have a permanent handle. Now your code will magically work. :) Best Wishes, -David Delaune

        M Offline
        M Offline
        Maxwell Chen
        wrote on last edited by
        #3

        Thank you! ;)

        Maxwell Chen

        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