classes pointers

Discussion in 'C++' started by adylevy, Jan 17, 2006.

  1. adylevy

    adylevy Guest

    i wanna write a class that handles key & mouse events,
    so any other class that wants to get mouse & keyboard events will write

    a pointer to this class, and then when an events occurs, this class
    will call
    all the subscribed classes ..

    i scatched something like that :

    class Handle_input{
    public:
    class(*registered)[10];

    void reg(void*val){
    registered[1]=(class*)val;
    }
    void run(){
    registered[1]->run();
    }
    };

    Hanle_input inpt; as a global.

    and then every class will run
    inpt.reg(this);

    its not working ;-) .. if anyone has an idea on how to get it close to
    working ... plz ..

    thanks.
    p.s
    it might not be the direction at all... but that's what i was thinking
    of, any ideas will be helping.
     
    adylevy, Jan 17, 2006
    #1
    1. Advertising

  2. adylevy wrote:
    > i wanna write a class that handles key & mouse events,
    > so any other class that wants to get mouse & keyboard events will write
    >
    > a pointer to this class, and then when an events occurs, this class
    > will call
    > all the subscribed classes ..
    >
    > i scatched something like that :
    >
    > class Handle_input{
    > public:
    > class(*registered)[10];
    >
    > void reg(void*val){
    > registered[1]=(class*)val;
    > }
    > void run(){
    > registered[1]->run();
    > }
    > };
    >
    > Hanle_input inpt; as a global.
    >
    > and then every class will run
    > inpt.reg(this);
    >
    > its not working ;-) .. if anyone has an idea on how to get it close to
    > working ... plz ..


    Oh, boy... You're biting off more than you can chew, obviously.

    Take a look at the "Listener" pattern. What you have is essentially a hub
    of communication that receives some input and forwards it to all who has
    registered to listen to it.

    > thanks.
    > p.s
    > it might not be the direction at all... but that's what i was thinking
    > of, any ideas will be helping.


    No, it's fine. You just need to get a better grasp of the object-oriented
    way of solving it. Like the fact that 'Handle_input' class needs to know
    the precise type of those to whom it forwards the information. Also, it
    is quite possible that you're starting from the wrong end. How is your
    'Handle_input' class going to be used? That (when you figure it out) will
    define the interface to it. It's possible that you'll retain the 'reg'
    and 'run' functions there, but it's possible that you're going to have to
    change them slightly... Start at the top, and drill down.

    V
     
    Victor Bazarov, Jan 17, 2006
    #2
    1. Advertising

  3. adylevy

    adylevy Guest

    the thing is that i want the Handle_input class to be general, in such
    a way that any class who has the run function, or the stop function
    will be able to register the class in the Handle_input class.

    i do know how to solve this issue by registering function names:
    void(*func) and then assigning functions in that way, but i want to
    assign a class name, so i can use it as if a button is pressed to
    inform all the classes who has "button_pressed" function that a button
    is pressed ..

    if there is no "easy" way to do so, i'll do it the "hard" way ..
    by making a dynamic list of functions to each function i want to run
    when something occurs.
     
    adylevy, Jan 17, 2006
    #3
  4. adylevy wrote:
    > the thing is that i want the Handle_input class to be general, in such
    > a way that any class who has the run function, or the stop function
    > will be able to register the class in the Handle_input class.


    That's fine. What you need is to define the base 'Handler' or 'Listener'
    class which will have only one member, the 'run' function. Any other
    class that can be a 'Listener' (or 'Handler') needs to derive from the
    base class and override the 'run' member. You're half way there.

    > [...]


    V
     
    Victor Bazarov, Jan 17, 2006
    #4
  5. adylevy

    adylevy Guest

    i got your idea, but yet, if i'll do it that way, i'll need to call
    this "run" function from every class, and not as i desired, to call one
    function that will call all the calsses functions

    thanks anyways.
     
    adylevy, Jan 17, 2006
    #5
  6. adylevy wrote:
    > i got your idea, but yet, if i'll do it that way, i'll need to call
    > this "run" function from every class, and not as i desired, to call one
    > function that will call all the calsses functions


    What do you mean by "to call this "run" function from every class"?

    What you're looking at is a loop in your 'run' function to call 'run'
    members for all objects that have registered at that moment:

    struct Handler {
    virtual void run() = 0;
    };

    class Handle_input {
    vector<Handler*> handlers;
    ...
    void run() {
    for (int i = 0, s = handler.size(); i < s; ++i)
    handlers->run();
    }
    };

    Now, you can make it more sophisticated if you have separate pools of
    handlers for every particular event, if you have handlers return some
    kind of code that would either cancel processing or let it continue,
    but that's not really important. What's important is that by using
    polymorphism, by calling a virtual function 'run' from a base class
    (and no casts as you can see), you achieve true OO processing of events
    in your system.

    What book on OOD are you reading?

    > thanks anyways.


    I don't like this expression. It's patronizing. If you don't feel I've
    helped you, don't thank me.

    V
     
    Victor Bazarov, Jan 17, 2006
    #6
  7. adylevy

    I V Guest

    adylevy wrote:
    > i got your idea, but yet, if i'll do it that way, i'll need to call
    > this "run" function from every class, and not as i desired, to call one
    > function that will call all the calsses functions


    No, you won't. What you have is a lot of objects with the same
    interface (i.e., they all have a member function called 'run'), but
    with different behavior. What you need, is a way to express this common
    interface - rather than having a pointer to any class in general, what
    you need is a pointer to 'any class with a run method'. You can express
    this using inheritance. For instance:

    class InputListener
    {
    public:
    virtual void run()
    {
    std::cout << "The basic run method" << std::endl;
    };

    class MyListener : public InputListener
    {
    public:
    virtual void run()
    {
    std::cout << "FirstListener run method" << std::endl;
    }
    };

    Making MyListener a child of InputListener specifies that MyListener
    has the same interface. So, you can use a pointer to MyListener
    anywhere you have specified a pointer to InputListener. You could have
    a handler class like:

    class HandleInput
    {
    std::vector<InputListener*> registry;

    public:

    void register(InputListener* il)
    {
    registry.push_back(il);
    }

    void run()
    {
    for( int i = 0; i != registry.size(); ++i )
    registry->run();
    }
    };

    Then, you can do something like this:

    int main()
    {
    HandleInput handler;
    MyListener ml;

    handler.register(&ml);
    handler.run();
    }

    This ability to use lots of different classes which all have the same
    interface is called 'polymorphism'. There's a tutorial which looks
    reasonably good at
    http://cplus.about.com/od/beginnerctutorial/l/aa120602a.htm
     
    I V, Jan 17, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phil
    Replies:
    1
    Views:
    664
    llewelly
    Sep 16, 2003
  2. muser
    Replies:
    3
    Views:
    776
    Ron Natalie
    Sep 18, 2003
  3. A
    Replies:
    3
    Views:
    467
    Alan Kelon
    Oct 29, 2003
  4. Replies:
    6
    Views:
    389
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    696
Loading...

Share This Page