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. How to get a pointer to an instance of a class...

How to get a pointer to an instance of a class...

Scheduled Pinned Locked Moved C / C++ / MFC
c++helptutorialquestion
3 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.
  • S Offline
    S Offline
    Sprudling
    wrote on last edited by
    #1

    I'm in a middle of writing my little own framework. I've created a class; lets call it MyFrameWorkApp, and all applications must have one and only one instance of this class (or more likely a class derived from this class). This class should, like in MFC, be instanciated somewhere at global level. (i.e. Before WinMain is called) Then I want WinMain to somehow get a pointer to this instance. This is how MFC does it...

    int AFXAPI AfxWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
    {
    ASSERT(hPrevInstance == NULL);

    int nReturnCode = -1;
    CWinThread\* pThread = AfxGetThread();
    CWinApp\* pApp = AfxGetApp();
    

    .
    .
    .

    Now, in my case both pThread and pApp points to the CWinApp instance, but that's normal I guess. Later in AfxWinMain, pThread->InitInstance() is called. I want the WinMain-function in my framework to do the same: Get a pointer to the MyFrameWorkApp instance, and call a particilar function. I've tried _lots_ of times to singelstep how MFC does this, my I can't figure it out. Can someone help me? :confused: Sprudling ;)

    P E 2 Replies Last reply
    0
    • S Sprudling

      I'm in a middle of writing my little own framework. I've created a class; lets call it MyFrameWorkApp, and all applications must have one and only one instance of this class (or more likely a class derived from this class). This class should, like in MFC, be instanciated somewhere at global level. (i.e. Before WinMain is called) Then I want WinMain to somehow get a pointer to this instance. This is how MFC does it...

      int AFXAPI AfxWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
      {
      ASSERT(hPrevInstance == NULL);

      int nReturnCode = -1;
      CWinThread\* pThread = AfxGetThread();
      CWinApp\* pApp = AfxGetApp();
      

      .
      .
      .

      Now, in my case both pThread and pApp points to the CWinApp instance, but that's normal I guess. Later in AfxWinMain, pThread->InitInstance() is called. I want the WinMain-function in my framework to do the same: Get a pointer to the MyFrameWorkApp instance, and call a particilar function. I've tried _lots_ of times to singelstep how MFC does this, my I can't figure it out. Can someone help me? :confused: Sprudling ;)

      P Offline
      P Offline
      Prem Kumar
      wrote on last edited by
      #2

      Use a Singleton Class, That will ensure that u can always get the singleton object thru a well known pointer or fn and that it is created only once. regards, Prem

      1 Reply Last reply
      0
      • S Sprudling

        I'm in a middle of writing my little own framework. I've created a class; lets call it MyFrameWorkApp, and all applications must have one and only one instance of this class (or more likely a class derived from this class). This class should, like in MFC, be instanciated somewhere at global level. (i.e. Before WinMain is called) Then I want WinMain to somehow get a pointer to this instance. This is how MFC does it...

        int AFXAPI AfxWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
        {
        ASSERT(hPrevInstance == NULL);

        int nReturnCode = -1;
        CWinThread\* pThread = AfxGetThread();
        CWinApp\* pApp = AfxGetApp();
        

        .
        .
        .

        Now, in my case both pThread and pApp points to the CWinApp instance, but that's normal I guess. Later in AfxWinMain, pThread->InitInstance() is called. I want the WinMain-function in my framework to do the same: Get a pointer to the MyFrameWorkApp instance, and call a particilar function. I've tried _lots_ of times to singelstep how MFC does this, my I can't figure it out. Can someone help me? :confused: Sprudling ;)

        E Offline
        E Offline
        Erik Funkenbusch
        wrote on last edited by
        #3

        Well, trying to copy MFC is fraught with danger and intrigue. MFC tends to do a lot of monkey business behind the scenes. For instance, quite often, instead of calling new, MFC will allocate memory for an object, then call the objects constructor manually. I guess it makes sense if you consider that MFC came from DOS originally, over 10 years ago. Anyways, how MFC does this is hidden in the constructor for CWinApp. In there, They do a few things, such as a module state variable = this (this being the CWinApp object) and doing some asserts that will assert if you try to create more than one instance. This isn't the best way to do this, however. The better way would be to make your MyFrameWorkApp a singleton object, which makes it impossible to create more than one instance. If you don't know what a singleton is, I suggest doing some web searches on it, or buying the most excellent book "Design Patterns". The reason you can't single-step into this is that the CWinApp's constructor get's called *BEFORE* WinMain. You have to put a breakpoint in CWinApp's constructor before you execute the app. -- Where are we going? And why am I in this handbasket?

        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