473,394 Members | 1,751 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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(*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.

Jan 17 '06 #1
6 1620
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
Jan 17 '06 #2
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.

Jan 17 '06 #3
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
Jan 17 '06 #4
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.

Jan 17 '06 #5
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[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?
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
Jan 17 '06 #6
I V
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[i]->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/beginnerct.../aa120602a.htm

Jan 17 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: | last post by:
I have this class ------------- class Component { /*class*/ Data *d; /*class*/ Draw *a; }; ------------- from "Component" derive classes like "TextBox", "Button", "Label", "ComboBox" etc from...
9
by: Jack | last post by:
Hello I have a library of calculationally intensive classes that is used both by a GUI based authoring application and by a simpler non-interactive rendering application. Both of these...
3
by: John J | last post by:
I requested help with some code in a previous thread, as requested in the feedback, below are the .cpp and .h files for all three classes (Entry, Race and Yacht). The three classes are all...
7
by: Sean J. Fraley | last post by:
This code illustrates what I'm confused about: template<typename T> class foo { public: template<typename U> void fooFunction(const foo<U>& x) {
5
by: Bilgehan.Balban | last post by:
Hi, I am currently brushing up my c++ knowledge and I would like to ask you about the differences between classes and C structs, in the function/method perspective. 1) Is it correct to say...
7
by: alternativa | last post by:
Hello, I have a few questions concerning classes. 1) Why some people use default constructos, i.e constructors with no parameters? To me it doesn't make any sense, is there something I should...
6
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
2
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the...
12
by: bgold | last post by:
Hey. I have a base class (SPRITE), and using this base class I have derived a large number of derived classes (PERSON, BULLET, MISSILE, etc.). Now, at a certain point in my program, I have a pair...
7
by: ademirzanetti | last post by:
Hi there !!! I would like to listen your opinions about inherit from a STL class like list. For example, do you think it is a good approach if I inherit from list to create something like...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.