none
How to call functions between 2 classes in the same header file.

    質問

  • 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
    How do I go about calling it?

    Thank you for replying

    • 編集済み Lofty88 2012年3月7日 3:33
    2012年3月7日 2:37

回答

  • 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.

     

    • 回答としてマーク Lofty88 2012年3月7日 7:06
    2012年3月7日 5:42

すべての返信

  • 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();

    2012年3月7日 3:36
  • 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?


    2012年3月7日 4:34
  • 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.

     

    • 回答としてマーク Lofty88 2012年3月7日 7:06
    2012年3月7日 5:42
  • Thank you, Scott.

    I've got it working. Had some problems with the pointer. After some researching, I managed it.

    Thanks again.

    2012年3月7日 7:06