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 parent's data members

Accessing the parent's data members

Scheduled Pinned Locked Moved C / C++ / MFC
11 Posts 4 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.
  • V Offline
    V Offline
    Vancouver
    wrote on last edited by
    #1

    I subclassed a control (actually, a CStatic) and want to access from it's functions some data members of the parent. Let's say the new class of the control is CMyOwnCtl, and the parent, the main dialog, is CMyOwnDlg. I am defining a pointer to the parent in CMyOwnCtl.h: CMyOwnDlg *Parent; This works only with #include "CMyOwnDlg.h" #include "CMyOwnCtl.h" in this sequence. However, CMyOwnDlg.h requires the definition of CMyOwnCtl, i.e. the includes have to be in the other sequence. I solved this by placing the pointer to the parent in the global definitions. However, I wonder if it could be done more elegantly.

    C J 2 Replies Last reply
    0
    • V Vancouver

      I subclassed a control (actually, a CStatic) and want to access from it's functions some data members of the parent. Let's say the new class of the control is CMyOwnCtl, and the parent, the main dialog, is CMyOwnDlg. I am defining a pointer to the parent in CMyOwnCtl.h: CMyOwnDlg *Parent; This works only with #include "CMyOwnDlg.h" #include "CMyOwnCtl.h" in this sequence. However, CMyOwnDlg.h requires the definition of CMyOwnCtl, i.e. the includes have to be in the other sequence. I solved this by placing the pointer to the parent in the global definitions. However, I wonder if it could be done more elegantly.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      You can put the includes in the cpp files, and then forward declare the class - so put class CMyOnwCtl; in the top of CMyOwnDlg.h and vice versa. This tells the compiler that a class exists, and it will find the details out during the compile process. I should add - this works because you have a *pointer* to CMyOwnDlg in CMyOwnCtl - this is fine, all the compiler needs to do when compiling the header, is allocate space for a pointer. If you put an instance of the object in the header, it would not work.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      V 1 Reply Last reply
      0
      • V Vancouver

        I subclassed a control (actually, a CStatic) and want to access from it's functions some data members of the parent. Let's say the new class of the control is CMyOwnCtl, and the parent, the main dialog, is CMyOwnDlg. I am defining a pointer to the parent in CMyOwnCtl.h: CMyOwnDlg *Parent; This works only with #include "CMyOwnDlg.h" #include "CMyOwnCtl.h" in this sequence. However, CMyOwnDlg.h requires the definition of CMyOwnCtl, i.e. the includes have to be in the other sequence. I solved this by placing the pointer to the parent in the global definitions. However, I wonder if it could be done more elegantly.

        J Offline
        J Offline
        John M Drescher
        wrote on last edited by
        #3

        One thing you could do is to make *Parent a class member of CMyOwnCtl and pass it in a constructor or a SetParent member. Actually you do not need to store the pointer though as you can always do a GetParent() in the control and DYNAMIC_CAST or (dynamic_cast) the CWnd pointer to CMyOwnDlg. I do this 90% of the time and use dynamic_cast syntax over DYNAMIC_CAST as my programs are compiled with RTTI and I avoid the MFC macros as much as I can.

        John

        V 1 Reply Last reply
        0
        • C Christian Graus

          You can put the includes in the cpp files, and then forward declare the class - so put class CMyOnwCtl; in the top of CMyOwnDlg.h and vice versa. This tells the compiler that a class exists, and it will find the details out during the compile process. I should add - this works because you have a *pointer* to CMyOwnDlg in CMyOwnCtl - this is fine, all the compiler needs to do when compiling the header, is allocate space for a pointer. If you put an instance of the object in the header, it would not work.

          Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

          V Offline
          V Offline
          Vancouver
          wrote on last edited by
          #4

          I tried this already before asking, but it causes an error, when assigning the address of the parent to the pointer. If ctl_MyControl is the member variable of the instance of the subclassed control, and Parent is defined in MyOwnCtl.h as class Parent; then ctl_MyControl.Parent = this; in the parent's OnInitDialog causes error C2440: '=' : cannot convert from 'class CMyOwnDlg *const ' to 'class $S217 *' However, if the pointer is defined (in the globals) as CMyOwnDlg *Parent; then Parent = this; works fine from OnInitDialog.

          C 1 Reply Last reply
          0
          • V Vancouver

            I tried this already before asking, but it causes an error, when assigning the address of the parent to the pointer. If ctl_MyControl is the member variable of the instance of the subclassed control, and Parent is defined in MyOwnCtl.h as class Parent; then ctl_MyControl.Parent = this; in the parent's OnInitDialog causes error C2440: '=' : cannot convert from 'class CMyOwnDlg *const ' to 'class $S217 *' However, if the pointer is defined (in the globals) as CMyOwnDlg *Parent; then Parent = this; works fine from OnInitDialog.

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            Vancouver wrote:

            ctl_MyControl.Parent = this;

            This means that ctl_MyControl is not a pointer, which means it won't work, like I said. If ctl_MyControl is a member variable, a forward declaration will only work if it's a pointer to an instance of your control.

            Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

            V 1 Reply Last reply
            0
            • C Christian Graus

              Vancouver wrote:

              ctl_MyControl.Parent = this;

              This means that ctl_MyControl is not a pointer, which means it won't work, like I said. If ctl_MyControl is a member variable, a forward declaration will only work if it's a pointer to an instance of your control.

              Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

              V Offline
              V Offline
              Vancouver
              wrote on last edited by
              #6

              Sorry, I mistyped it in the post. Of course I declared it as a pointer and it created the mentioned error.

              C 1 Reply Last reply
              0
              • V Vancouver

                Sorry, I mistyped it in the post. Of course I declared it as a pointer and it created the mentioned error.

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #7

                Was the code to set it in the header or the cpp file ? I've done this many times, it's definately the correct way to do things. http://www.google.com.au/search?sourceid=navclient&ie=UTF-8&rlz=1T4ADBS_enAU225AU226&q=C%2b%2b+forward+declare[^] for google hits on the subject of forward declaring. I don't do c++ nowadays, perhaps I'm missing a detail.

                Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                V 1 Reply Last reply
                0
                • C Christian Graus

                  Was the code to set it in the header or the cpp file ? I've done this many times, it's definately the correct way to do things. http://www.google.com.au/search?sourceid=navclient&ie=UTF-8&rlz=1T4ADBS_enAU225AU226&q=C%2b%2b+forward+declare[^] for google hits on the subject of forward declaring. I don't do c++ nowadays, perhaps I'm missing a detail.

                  Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                  V Offline
                  V Offline
                  Vancouver
                  wrote on last edited by
                  #8

                  class *Parent; is in CMyOwnCtl.h. If it was not there, I would not have any problem: in the function itself CMyOwnDlg can be used. because both headers are known there (and then John M. Drescher's suggestion works).

                  R 1 Reply Last reply
                  0
                  • J John M Drescher

                    One thing you could do is to make *Parent a class member of CMyOwnCtl and pass it in a constructor or a SetParent member. Actually you do not need to store the pointer though as you can always do a GetParent() in the control and DYNAMIC_CAST or (dynamic_cast) the CWnd pointer to CMyOwnDlg. I do this 90% of the time and use dynamic_cast syntax over DYNAMIC_CAST as my programs are compiled with RTTI and I avoid the MFC macros as much as I can.

                    John

                    V Offline
                    V Offline
                    Vancouver
                    wrote on last edited by
                    #9

                    This appeared to be all right, at least syntactically. However, CMyOwnDlg *Parent = dynamic_cast (GetParent()); in the function of the control caused an error: RTDynamicCast threw an exception.

                    J 1 Reply Last reply
                    0
                    • V Vancouver

                      class *Parent; is in CMyOwnCtl.h. If it was not there, I would not have any problem: in the function itself CMyOwnDlg can be used. because both headers are known there (and then John M. Drescher's suggestion works).

                      R Offline
                      R Offline
                      Rajkumar R
                      wrote on last edited by
                      #10

                      When you use forward declaration. The forward declared variable cannot be accessed in the declaration file, since compiler only knows the pointer and allocates the size of pointer when you access it, compiler won't have enough information to do this. ctl_MyControl.Parent = this; Is this statement is in declaration file, if so, use this in a function definition is cpp file. Since the dialog is immediate parent, you know the level of the parent class, you can use john's method (dynamic cast the parent window). Best Regards Raj

                      1 Reply Last reply
                      0
                      • V Vancouver

                        This appeared to be all right, at least syntactically. However, CMyOwnDlg *Parent = dynamic_cast (GetParent()); in the function of the control caused an error: RTDynamicCast threw an exception.

                        J Offline
                        J Offline
                        John M Drescher
                        wrote on last edited by
                        #11

                        Did you set the compiler option to enable RTTI (Run time type info)? If not use DYNAMIC_CAST as this will work without the compiler option.

                        John

                        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