classes pointers

A

adylevy

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

Victor Bazarov

adylevy said:
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
 
A

adylevy

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

Victor Bazarov

adylevy said:
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
 
A

adylevy

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

Victor Bazarov

adylevy said:
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
 
I

I V

adylevy said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top