How to call functions between 2 classes in the same header file.
-
Wednesday, March 07, 2012 2:37 AM
Hi,
I know this sounds easy. But I am not sure of what to do. I am using MFC C++
I have 2 classes in one header file:class Class1 : public CEdit { public: void OnMouseEvent(MouseEvent mouseEvent, CPoint point); }; class Class2: public CDialog { void SetMinMax(); };
I wanted to call SetMinMax() from OnMouseEvent(...), where Class 2 is the base class
Thank you for replying
How do I go about calling it?
All Replies
-
Wednesday, March 07, 2012 3:36 AM
You need a pointer to the object you want to call. Here's one way to get such a pointer, assuming that the edit control is in the dialog:
Class2* p = (Class2*)GetParent();
p->SetMinMax(); -
Wednesday, March 07, 2012 4:34 AM
I'm sorry.
I've tried but it cannot works.I think its because I created my own class.
It gave me this error:
error C2039: 'GetParent' : is not a member of 'Class1'
I thought even if i create my own class, I am able to use the "getparent".
class Class1: My own class { public: void OnMouseEvent(MouseEvent mouseEvent, CPoint point); }; class Class2: public CDialog { void SetMinMax(); };Why isit that if i create my own class and do it in the way you mention, it will give me this error, and how do i go about solving it?
-
Wednesday, March 07, 2012 5:42 AM
In your first post you showed Class1 derived from CEdit. If your class is derived from CEdit (and the edit control is part of the dialog) then you would have an inherited GetParent member function.
If your class is not a CEdit then put a Class2* m_p2 variable in it. Initialize that pointer when you create the Class1 object, or sometime before it needs to call the dialog function.
If you don't understand how, explain and show where and how you create the Class1 object.
- Marked As Answer by Lofty88 Wednesday, March 07, 2012 7:06 AM
-
Wednesday, March 07, 2012 7:06 AM
Thank you, Scott.
I've got it working. Had some problems with the pointer. After some researching, I managed it.
Thanks again.

