一个类中的一个函数如何能够从一个对象到另一个对象做各种事情? [英] how one function in one class can do various thing from one object to onother?

查看:63
本文介绍了一个类中的一个函数如何能够从一个对象到另一个对象做各种事情?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个类,并且在其中我们有一个函数(例如OnTouchListner),当这个方法调用时,来自这个类的任何对象做各种事情。

任何人都知道如何解决这个问题。

来自我的问题的真实例子是MFC或任何opreation系统所做的工作。

以下代码是我项目的简单形状和摘要代码



suppose that we have a class and in it we have a function(such as OnTouchListner) and any object from this class do various thing when this methode called.
any one know how can i resolve this problem.
real example from my question is job that MFC or any opreation system do.
below code is simple shape and summary code of my project

namespace MyOS
{
    namespace GUI
    {
        class CControl
        {
            public :
            void OnTouchListner(void);  //my wrong solution is void(*OnTouchListner)(void);
        };
        class CButton : public CControl
        {
            public :
            ...
        };


    }
    namespace Core
    {
        class CKernel
        {
            private :
            EventListner();
        };

        class CForm
        {
            public :
            CLinkList<GUI::CControl> controls;
            ...
        };

    }
    namespace StandardDialoges
    {
        class CKeyPad : public Core::CForm
        {
            public :
            void Designer(void) {
                /*my wrong solution
                this->btnZ->OnTouchListner = &CKeyPad::btnZ_OnTouch_Listner;
                and call this in EventListner
                */
            }
            GUI::CButton btnZ;
            void btnZ_OnTouch_Listner(void)    {}
        };
    }



现在我的问题是来自控件的调用相关函数,当事件列表器检测到一个新事件,如ontouch事件。

i thought该函数指针是这个问题的解决方案,但现在我认为这是错误的方式。

请帮我找到解决问题的方法。

非常感谢。


now my problem is call related function from controls when event listner detect a new event such as ontouch event.
i thought that function pointer is solution of this problem but now i that it is wrong way.
please help me to find solution of my problem.
with very thank.

推荐答案

和其他两位受访者一样,我不确定我是否理解这个问题。如果您询问是否有一个类的函数与另一个类类型的对象一起工作,您可以将指针或引用传递给另一个对象,并且您将在类函数中使用它。
Like the other two respondents, I'm not sure I understand the question. If you are asking if you have have a function of one class work with an object of another class type, you can pass a pointer or reference to that other object and you'll have it available inside your class function.


显然,您的GUI实现具有不同类型的控件(例如 CButton ),这些控件都是从公共基类派生的, CControl 。此外,你有一个类 CForm ,它是这种GUI控件的容器。在旁注中,我建议你将该类放入 GUI 命名空间。



此外你还有一个活动监听器,其目的是检查输入消息和其他类型的事件,并将它们与可能是这些消息的目标的表单和GUI控件相关联。



那里你必须要做的事情是:

1.由于 CControl 应该用作基础分支,它必须 / u>有一个虚拟的析构函数!根据您的设置,编译器可能会为您创建一个默认构造函数,但要确保您自己定义一个。

2.同样,因为 CControl 应该是一个基类,你的 CForm 类必须将它的控件存储为指向 CControl ,而不是 CControl 实例!

3.同样,同样,因为 CControl 用作基类,其他控件派生自它,应该在基础中声明为不同控件显示不同行为的方法 virtual class,并在派生类中有一个虚拟覆盖。

4.事件监听器必须向所有相关控件发送事件信息。它可能不也不应该知道哪个控件实际上对任何特定事件感兴趣,因此基本上有两种方法可以实现这一点:

(a)将所有事件广播到所有控件并让他们决定 - 这增加了防止多个控件对单个事件做出反应的问题(除非这是目的)

(b)让控件'告诉'事件监听器他们感兴趣的是什么事件,所以监听器仅将事件发送到相关控件。这个原则被称为观察者模式 [ ^ ]
Apparently your implementation of a GUI features different types of controls (such as CButton) that are all derived from the common base class, CControl. Moreover, you have a class CForm that is a container for such GUI controls. On a sidenote I would advise you to put that class into the GUI namespace.

Further you have an event listener whose purpose is to check on input messages and other kinds of events, and relate them to the forms and GUI controls that may be the target of these messages.

There are a couple of things you must do:
1. Since CControl is supposed to be used as a base clase, it must have a virtual destructor! Depending on your settings, the compiler may create a default constructor for you, but to be sure you should define one yourself.
2. Similarly, since CControl is supposed to be a base class, your CForm class must store its controls as a list of pointers to CControl, not as CControl instances!
3. Again, similarly, since CControl is used as a base class and other controls derive from it, the methods that are supposed to show different behaviour for different controls must be declared virtual in the base class, and have a virtual override in the derived class.
4. The event listener must send event information to all relevant controls. It may not and should not need to know which control is actually interested in any particular event, so there's basically two ways to achieve this:
(a) broadcast all events to all controls and let them decide - this adds the problem of preventing multiple controls to react to a single event (unless that was the purpose)
(b) have the controls 'tell' the event listener what events they're interested in, so the listener only sends out the events to the relevant controls. This principle is known as the Observer Pattern[^]


这篇关于一个类中的一个函数如何能够从一个对象到另一个对象做各种事情?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆