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. pointer to the app

pointer to the app

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

    Hi i've got an object that I want to use with a number of dialogues, so I declared it in my App class, and I was then going to use a pointer to the app object to acess it in all my dialogues. How do I get the pointer to the app to access the object? At the moment I have something like CtheApp* m_pApp; but how do I initialise the pointer to point at the app? I tried something like m_pApp = &theApp; but I just get the compiler saying that theApp is an undeclared variable but I thought it was a global. Can someone enlighten me as to the way to proceed? Thanks Andy

    R 1 Reply Last reply
    0
    • A andyg 101

      Hi i've got an object that I want to use with a number of dialogues, so I declared it in my App class, and I was then going to use a pointer to the app object to acess it in all my dialogues. How do I get the pointer to the app to access the object? At the moment I have something like CtheApp* m_pApp; but how do I initialise the pointer to point at the app? I tried something like m_pApp = &theApp; but I just get the compiler saying that theApp is an undeclared variable but I thought it was a global. Can someone enlighten me as to the way to proceed? Thanks Andy

      R Offline
      R Offline
      RChin
      wrote on last edited by
      #2

      CMyApp* theApp = static_cast<CMyApp*>(AfxGetApp());


      "..Even my comments have bugs!"
      Inspired by Toni78

      A 1 Reply Last reply
      0
      • R RChin

        CMyApp* theApp = static_cast<CMyApp*>(AfxGetApp());


        "..Even my comments have bugs!"
        Inspired by Toni78

        A Offline
        A Offline
        andyg 101
        wrote on last edited by
        #3

        nice one, thanks, but what is the static_cast for can't I just use AfxGetApp on its own? Andy

        D 1 Reply Last reply
        0
        • A andyg 101

          nice one, thanks, but what is the static_cast for can't I just use AfxGetApp on its own? Andy

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

          Yes, but it will return you a CWinApp pointer, not a CtheApp pointer. That's why it must be cast to the appropriate type.

          R 1 Reply Last reply
          0
          • D David Crow

            Yes, but it will return you a CWinApp pointer, not a CtheApp pointer. That's why it must be cast to the appropriate type.

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

            DavidCrow wrote: Yes, but it will return you a CWinApp pointer, not a CtheApp pointer Exactly. AfxGetApp() Returns a CWinApp class pointer. If you want to reference your specific class, you have to cast the returned pointer to your application class. This will allow you to access your member functions. The olden way of casting used to be of the form:

            CMyClass* pClass = (CMyClass*)(ParentClass);
            
            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