classes pointers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • adylevy

    #1

    classes pointers

    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(*register ed)[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.

  • Victor Bazarov

    #2
    Re: classes pointers

    adylevy wrote:[color=blue]
    > 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(*register ed)[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 ..[/color]

    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.
    [color=blue]
    > thanks.
    > p.s
    > it might not be the direction at all... but that's what i was thinking
    > of, any ideas will be helping.[/color]

    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

    Comment

    • adylevy

      #3
      Re: classes pointers

      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.

      Comment

      • Victor Bazarov

        #4
        Re: classes pointers

        adylevy wrote:[color=blue]
        > 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.[/color]

        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.
        [color=blue]
        > [...][/color]

        V

        Comment

        • adylevy

          #5
          Re: classes pointers

          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.

          Comment

          • Victor Bazarov

            #6
            Re: classes pointers

            adylevy wrote:[color=blue]
            > 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[/color]

            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[i]->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?
            [color=blue]
            > thanks anyways.[/color]

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

            V

            Comment

            • I V

              #7
              Re: classes pointers

              adylevy wrote:[color=blue]
              > 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[/color]

              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 << "FirstListe ner 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<Inp utListener*> registry;

              public:

              void register(InputL istener* il)
              {
              registry.push_b ack(il);
              }

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

              Then, you can do something like this:

              int main()
              {
              HandleInput handler;
              MyListener ml;

              handler.registe r(&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


              Comment

              Working...