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. accessing the base class

accessing the base class

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialhelp
7 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
    satadru
    wrote on last edited by
    #1

    hi , i have a dialog based application having a class for example CDialog. in the on button click event iam loading another dialog as follows: m_pModeless= new dlg; Then after getting the safeHwnd : m_pModeless->Create(......); where dlg is the class of the new dialog and m_pModeless has been declared in the header file as dlg *m_pModeless. Now i want to access the member variables from this child window to the main window i.e from one class to the other. I'am a bit confused of how to go about it ... please help. Thanking you. Satadru.

    A E 2 Replies Last reply
    0
    • S satadru

      hi , i have a dialog based application having a class for example CDialog. in the on button click event iam loading another dialog as follows: m_pModeless= new dlg; Then after getting the safeHwnd : m_pModeless->Create(......); where dlg is the class of the new dialog and m_pModeless has been declared in the header file as dlg *m_pModeless. Now i want to access the member variables from this child window to the main window i.e from one class to the other. I'am a bit confused of how to go about it ... please help. Thanking you. Satadru.

      A Offline
      A Offline
      Antti Keskinen
      wrote on last edited by
      #2

      I assume you are attempting one of the following procedures: 1. Accessing the newly created dialog from the button click event 2. Accessing the class running the button event from the newly created dialog For case #1, the answer is simple: you already have a pointer to the class, and it is properly initialized by using new. You can use this pointer to alter the data members as you see fit. For case #2, you need to specify the calling class as the parent of the newly created dialog. For modeless dialog creation, the calling convention would be CDialog::Create( UINT uiTemplateID, CWnd* pParent ). Specifying the this keyword into the parent pointer would designate the calling class as the parent of the newly created modeless dialog. Further in case #2, you can access the parent window by using GetParent method, which will return a pointer to the parent class's CWnd. Because you have derived from CDialog, and because CDialog is derived from CWnd, you can use the pointer returned by GetParent and cast it down to the appropriate class. Understanding this, here is a code fragment, which will do the casting operation. Remember that if you put this casting operation into a function inside the newly created dialog, you must include the header file of the parent dialog into the implementation file. Otherwise the casting operation is unable to determine the type of the variable. In here, we assume that the parent class (which handles the button click), is called CParentDialog and the child dialog (newly created one) is called CChildDialog.

      // In the beginning of the implementation, include the header of parent
      #include "ParentDialog.h"

      ...

      // Further down the road, a member function of CChildDialog, get the parent class and cast it down
      CParentDialog* pParentDialog = DYNAMIC_DOWNCAST( CParentDialog, GetParent() );

      // Use the pointer to access data member/methods
      pParentDialog->SomeMethod( someParams );
      pParentDialog->SomeData = SomeValue;

      This functionality comes directly from the way MFC is designed. Have a look at the inheritance map (hiearchy chart). In C++, it is always possible to cast a pointer from the base class to the derived class, and in MFC,

      S 1 Reply Last reply
      0
      • S satadru

        hi , i have a dialog based application having a class for example CDialog. in the on button click event iam loading another dialog as follows: m_pModeless= new dlg; Then after getting the safeHwnd : m_pModeless->Create(......); where dlg is the class of the new dialog and m_pModeless has been declared in the header file as dlg *m_pModeless. Now i want to access the member variables from this child window to the main window i.e from one class to the other. I'am a bit confused of how to go about it ... please help. Thanking you. Satadru.

        E Offline
        E Offline
        ElCachubrey
        wrote on last edited by
        #3

        Hi You should pass a pointer on the main window on constuctor of child dlg. //Forward declaration class CMainDlg; class CChildDlg : public CDialog{ CChildDlg(CMainDlg* p_main_dlg):CDialog(...){ this->p_main_dlg = p_main_dlg; //Now throught this->p_main_dlg you can get //allother public members of main dlg } private: CMainDlg* p_main_dlg; }; class CMainDlg : public CDialog{ ... paublic: int var; void CraeteChild(){ CChildDlg dlg(this); dlg.DoModal(); } };

        1 Reply Last reply
        0
        • A Antti Keskinen

          I assume you are attempting one of the following procedures: 1. Accessing the newly created dialog from the button click event 2. Accessing the class running the button event from the newly created dialog For case #1, the answer is simple: you already have a pointer to the class, and it is properly initialized by using new. You can use this pointer to alter the data members as you see fit. For case #2, you need to specify the calling class as the parent of the newly created dialog. For modeless dialog creation, the calling convention would be CDialog::Create( UINT uiTemplateID, CWnd* pParent ). Specifying the this keyword into the parent pointer would designate the calling class as the parent of the newly created modeless dialog. Further in case #2, you can access the parent window by using GetParent method, which will return a pointer to the parent class's CWnd. Because you have derived from CDialog, and because CDialog is derived from CWnd, you can use the pointer returned by GetParent and cast it down to the appropriate class. Understanding this, here is a code fragment, which will do the casting operation. Remember that if you put this casting operation into a function inside the newly created dialog, you must include the header file of the parent dialog into the implementation file. Otherwise the casting operation is unable to determine the type of the variable. In here, we assume that the parent class (which handles the button click), is called CParentDialog and the child dialog (newly created one) is called CChildDialog.

          // In the beginning of the implementation, include the header of parent
          #include "ParentDialog.h"

          ...

          // Further down the road, a member function of CChildDialog, get the parent class and cast it down
          CParentDialog* pParentDialog = DYNAMIC_DOWNCAST( CParentDialog, GetParent() );

          // Use the pointer to access data member/methods
          pParentDialog->SomeMethod( someParams );
          pParentDialog->SomeData = SomeValue;

          This functionality comes directly from the way MFC is designed. Have a look at the inheritance map (hiearchy chart). In C++, it is always possible to cast a pointer from the base class to the derived class, and in MFC,

          S Offline
          S Offline
          satadru
          wrote on last edited by
          #4

          hi thanks for your help ; but theres a problem:- inspite of including the header file of the main dialog viz CMainDlg its still giving me an error saying: 'classCMainDlg': is not a member of 'CMainDlg' Could you please tell me why is this happening.:(( thanking you, Satadru

          A 1 Reply Last reply
          0
          • S satadru

            hi thanks for your help ; but theres a problem:- inspite of including the header file of the main dialog viz CMainDlg its still giving me an error saying: 'classCMainDlg': is not a member of 'CMainDlg' Could you please tell me why is this happening.:(( thanking you, Satadru

            A Offline
            A Offline
            Antti Keskinen
            wrote on last edited by
            #5

            What is this 'viz' ? "Which is" ? The error you are describing is related to the CMainDlg itself. You are referring to a member classCMainDlg somewhere in your code, and this member is not declared in the header file of the class. See the header file of CMainDlg for details, especially check the line where the error is. The case #2 I posted back in my previous message did not tell you to add any member to the parent (CMainDlg) class. If you added members there, then you have done something wrong yourself. In conclusion: this error has nothing to do with the method I described. It is happening because a) you have done something wrong yourself or b) have not understood my description properly. I would ask you to post the line where this error is happening and the context where it is in. I believe that it might be in the line where you create the modeless dialog box. Remember, that the 'this' pointer always points to the current object that is executing a method. For example, if you had an object of a class created, returning 'this' from a function would return the address of the object. Check your code and post the line where the error is happening. You probably have just mistyped something. -Antti Keskinen ---------------------------------------------- The definition of impossible is strictly dependant on what we think is possible.

            S 1 Reply Last reply
            0
            • A Antti Keskinen

              What is this 'viz' ? "Which is" ? The error you are describing is related to the CMainDlg itself. You are referring to a member classCMainDlg somewhere in your code, and this member is not declared in the header file of the class. See the header file of CMainDlg for details, especially check the line where the error is. The case #2 I posted back in my previous message did not tell you to add any member to the parent (CMainDlg) class. If you added members there, then you have done something wrong yourself. In conclusion: this error has nothing to do with the method I described. It is happening because a) you have done something wrong yourself or b) have not understood my description properly. I would ask you to post the line where this error is happening and the context where it is in. I believe that it might be in the line where you create the modeless dialog box. Remember, that the 'this' pointer always points to the current object that is executing a method. For example, if you had an object of a class created, returning 'this' from a function would return the address of the object. Check your code and post the line where the error is happening. You probably have just mistyped something. -Antti Keskinen ---------------------------------------------- The definition of impossible is strictly dependant on what we think is possible.

              S Offline
              S Offline
              satadru
              wrote on last edited by
              #6

              hi , i have tried it out ..but the same problem exixts... i cudn't figure out where its going wrong ..can i have your email id so that i'll attach a small code snippet which is posing me the problem. I gratefuly appreciate your kind gesture. Thanking you. Satadru.

              A 1 Reply Last reply
              0
              • S satadru

                hi , i have tried it out ..but the same problem exixts... i cudn't figure out where its going wrong ..can i have your email id so that i'll attach a small code snippet which is posing me the problem. I gratefuly appreciate your kind gesture. Thanking you. Satadru.

                A Offline
                A Offline
                Antti Keskinen
                wrote on last edited by
                #7

                Just paste the code here, and I will analyse it promptly. I prefer not to give my e-mail address away on public forums, they tend to end up to mischiveous hands :suss: Besides, if your code had something so important that you couldn't post it here, how could you send it on e-mail either ? :) -Antti Keskinen ---------------------------------------------- The definition of impossible is strictly dependant on what we think is possible.

                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