Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

misterX

how to raise and catch events in c++ ?

This topic is 5893 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m used to VB and now that i''m going over to c++, i''ve several questions: In VB, you ca use RaiseEvent myEvent(...) to raise a custom event from your object/class. What''s the equivalent in c++? and how do you catch it? thanks.

Share this post


Link to post
Share on other sites
Advertisement
I''m not familiar with VB, but could it be you''re looking for try, catch and throw?

Crispy

Share this post


Link to post
Share on other sites
I''m not very used with c++ but try,catch,throw is used to throw exeption when calling a function, isn''t it?

I would like to send an event so that a method in the object''s owner is executed.

Share this post


Link to post
Share on other sites
quote:
Original post by misterX
I''m not very used with c++ but try,catch,throw is used to throw exeption when calling a function, isn''t it?

I would like to send an event so that a method in the object''s owner is executed.


You probably want to use function pointers. C++ does not have a built-in ''event'' system.

You can also do it with inheritance and polymorphism, which would somewhat resemble how VB does it. But this would involve writing an additional class, as well as learning all about inheritance and virtual functions.

If you just want to call some arbitrary function, then use function pointers.

If you can, find some help on how to store and use a pointer to a member function. Then you should be able to do what you are wanting to do.

I''m not really familiar with it enough to teach it to anyone. It''s similar to a callback, if you know what that is.

--TheMuuj

Share this post


Link to post
Share on other sites
I don''t know exactly what you want, but I''ll give it a try.

You could use a method like, say event() in your class. If you make all your events objects that are instances of a certain class that provides for identification of the event type, you just make your event() method look up the event type and act accordingly.

If you just want to be able to say something like raiseEvent(someEventImadeUp) from anywhere, you''d have to do some more complex stuff like create a central list that contains pointers to all objects able to act on signal.

I need some sugar, thinking is difficult at the moment. So if the above makes absolutely no sense, please forgive me.

Share this post


Link to post
Share on other sites
hmm...
it seems that events don't really exist in c++ !?
i've also searched on the web but didn't find precise stuff about it.

then, if there is no way to simply manage events (like in VB), another way must be found.
Is this the right way to do it (rigoursly):

let's call the classes A and B, where A is owning instance(s) of B. So if B send a so called event, it must arrive to A.
So, the way i thought to do it is:
- Creating a 'MyClassEventReceiver' class wich contains method which can be called to raise the event.
- a Struct or Class 'MyObjectEvent1' which contain the event's informations.
So every A class owning B must extend 'MyClassEventReceiver'. And the B class must contain a pointer to a 'MyClassEventReceiver' type object. And then call methods in 'MyClassEventReceiver' to send the corresponding event.

Is it the right way to do it?

[edited by - misterX on June 27, 2002 10:50:32 AM]

Share this post


Link to post
Share on other sites
Events and event sinks are a part of the COM framework, and are made fairly transparent behind VB. There is no directly equivalent concept in Standard C++. The question that has to be asked is what problem are you trying to solve that you think requires an eventing mechanism?

Share this post


Link to post
Share on other sites
Well...
in fact, it's not a problem.
I've some experience in game programming, small projects and OOP. And i'm now going on to some little bigger projects. But most of all, i want to construct them well!
And events are an important part of it.

for example, imagine the following object structure:
>Scheduler
--->IO
------>IOinput
------>...
--->MenuScreen
------>MenuInputInterpretor
------>...
--->GameScreen
------>GameInputInterpretor
------>...
--->other "screens"...


And now, we want to make the game... Let's say we project to implement IO and its components using DirectX. But in order to save time and to see results of the program as it is yet, we'll implement it with a simple visual window.


The MenuInputInterpretor and GameInputInterpretor are used to interpret what they receive from the input source (IOinput), that means (for example) it recieves mouseClic on (321,456) and give out 'Button32pressed' (or nothing).
Let's look at the way all that happens:
The myWindowClass handles the notification MyWindowClass::OnLButtonDown(UINT nFlags, CPoint point)
-> call myIOinput::mouseDown(button,x,y)
-> myIOinput calls myAttachedInputInterpretor::mouseDown(button,x,y) (this is the same as previously but would be different if using DX)
-> XXXInputInterpretor calls myParentClass::weaponSelected(...) (for example)

Well, it looks maybe a little complicated at first sight!
It's mainly because i want you to show the long flow events can generate.

And now, the way to implement all this. 3 possibilities i see for the moment:
1) You SUPPOSE all owner classes have the needed methods to call
2) Each parent object implement a specific Interface
3) Each parent object inherits these methods from an 'EventReceiver' class.


These 3 solutions have the same big problem:
the event methods can't be private! But they should be forbidden to call them externaly!
The myMenuScreen should not be able to call the method MenuInputInterpretor::mouseDown(button,x,y) !!!


Of course, this will not break down anything. It's more a question of rigour.

So, what's the solution ?


To simplify all this:
a class A and a class B. myA is owning myB. How can myB "activate" a reaction in A without making this function public ?


(and it's so simple in VB !!!)

[edited by - misterX on June 27, 2002 1:32:14 PM]

Share this post


Link to post
Share on other sites
the friend keyword


  
class A
{
public:
// This makes all members (public and private)

// of class A accessible from class B

friend class B;
class B
{
public:
void CallMethodOfA(A& objA) { objA.SomeMethod(); }
private:
int somedata;
};
private:
void SomeMethod() { /* do something here in response to B */ }
};

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!