Conceptual Question
-
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,
-
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,
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
-
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,
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
-
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
-
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) {}
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.
-
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,
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; }