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. Conceptual Question

Conceptual Question

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
6 Posts 5 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.
  • L Offline
    L Offline
    LCI
    wrote on last edited by
    #1

    I have a dialog class and another class that inherits from Cwnd. Call them class A and class B. They do not inherit from each other. However, i need for class B to have access to a member var in class A. Is the corect thing to do to have a pointer somewhere global to class A so that class B can use it when it needs it? If so, can someone help me with the syntax. I have tried different things and have been unsuccessful. Thanks,

    C M F 3 Replies Last reply
    0
    • L LCI

      I have a dialog class and another class that inherits from Cwnd. Call them class A and class B. They do not inherit from each other. However, i need for class B to have access to a member var in class A. Is the corect thing to do to have a pointer somewhere global to class A so that class B can use it when it needs it? If so, can someone help me with the syntax. I have tried different things and have been unsuccessful. Thanks,

      C Offline
      C Offline
      charlieg
      wrote on last edited by
      #2

      Please think about your design... otherwise, declare the classes "friend" as appropriate. For example, if you want to grant class A access to class B private areas, class B must state that class A is a friend. Run-time - if you want access to the live data, well, you'd need to think about where you want the data and how the object instances know of the other.

      Charlie Gilley Will program for food... Whoever said children were cheaper by the dozen... lied. My son's PDA is an M249 SAW. My other son commutes in an M1A2 Abrams

      1 Reply Last reply
      0
      • L LCI

        I have a dialog class and another class that inherits from Cwnd. Call them class A and class B. They do not inherit from each other. However, i need for class B to have access to a member var in class A. Is the corect thing to do to have a pointer somewhere global to class A so that class B can use it when it needs it? If so, can someone help me with the syntax. I have tried different things and have been unsuccessful. Thanks,

        M Offline
        M Offline
        Maximilien
        wrote on last edited by
        #3

        What is the "physical" relationship between class A and B ? can't you pass a pointer a A to class B ?


        Maximilien Lincourt Your Head A Splode - Strong Bad

        L 1 Reply Last reply
        0
        • M Maximilien

          What is the "physical" relationship between class A and B ? can't you pass a pointer a A to class B ?


          Maximilien Lincourt Your Head A Splode - Strong Bad

          L Offline
          L Offline
          LCI
          wrote on last edited by
          #4

          I can do that but i think that the compiler gets confused when i do a double include... e.g. Class A : public CDialog #include B.h SetDlgPtr(this); Class B : public CWnd #include A.h void B::SetDlgPtr(CA * pCA) {}

          B 1 Reply Last reply
          0
          • L LCI

            I can do that but i think that the compiler gets confused when i do a double include... e.g. Class A : public CDialog #include B.h SetDlgPtr(this); Class B : public CWnd #include A.h void B::SetDlgPtr(CA * pCA) {}

            B Offline
            B Offline
            BambooMoon
            wrote on last edited by
            #5

            This is a common problem. The solution is simple. In one of the .h files, don't #include the header file of the other class. Instead just put a forward declaration. For example, in b.h, remove #include A.h and put "Class A;". Then in b.cpp put the #include a.h.

            1 Reply Last reply
            0
            • L LCI

              I have a dialog class and another class that inherits from Cwnd. Call them class A and class B. They do not inherit from each other. However, i need for class B to have access to a member var in class A. Is the corect thing to do to have a pointer somewhere global to class A so that class B can use it when it needs it? If so, can someone help me with the syntax. I have tried different things and have been unsuccessful. Thanks,

              F Offline
              F Offline
              flippydeflippydebop
              wrote on last edited by
              #6

              what about creating a static pointer in the classes that you want to be able to use from any other class/function. then all you need to do is include the header for the class and voila!. For example: //// hello.h class One { public: One(void); ~One(void); public: void oneFunction(); int oneX; }; class Two { public: Two(void); ~Two(void); public: static Two* m_ptrTwo; // <-- declare a static pointer to ourself {Two*) void twoFunction(); int twoX; }; and the body... //// hello.cpp #include "hello.h" Two*Two::m_ptrTwo = NULL; // <-- define the static pointer for class Two here.. set it to NULL // constructor for class Two Two::Two(void) { m_ptrTwo = this; // <-- setup the pointer to point to.... itself!!! } Now we can use the pointer 'm_ptrTwo' to call anything in the public interface of the class Two. For example: One::oneFunction() { // Now here we will use the pointer from class Two to call anything in the public interface. Two::m_ptrTwo->twoFunction(); Two::m_ptrTwo->twoX = 1234; }

              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