Click here to Skip to main content
15,887,336 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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

C++
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)    {}
        };
    }

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.
Posted
Comments
Sergey Alexandrovich Kryukov 14-Mar-14 0:03am    
I looks like the question about nothing. No problem is formulated.
—SA
Stefan_Lang 3-Apr-14 4:58am    
Looks like he's trying to implement an event-driven OS and needs help with virtual inheritance and the Observer Pattern.
R.B. 14-Mar-14 8:03am    
Research the command pattern, it could apply to what you are trying to do.
Stefan_Lang 3-Apr-14 4:59am    
Obersver Pattern, not command. At least until he tries to 'undo' touch events ;-p
Argonia 2-Apr-14 3:17am    
I think he is trying to ask how to use virtual functions. As far as i understand he is trying to execute different code with the same function for different objects.
Please see what virtual functions are and how they work

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.
 
Share this answer
 
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[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900