# Can I use events in C++ like in C#?

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

## Recommended Posts

I have a game object manager class that manages a list of all the game objects. I also have a physics manager class that stores the collision geometry for each object. When the physics manager does collision detection, I'd like for it to raise an event to the main application class with the ID's of the two objects colliding, so I can get them from the game object manager and call their CollisionResponse functions. In C# I know how I would do this, but I'm using C++ and having some trouble. Is this even possible, or am I taking the wrong approach?

##### Share on other sites
There's no built-in construct for that in C++, you'll have to implement it yourself using a list of callbacks.

##### Share on other sites
"A list of callbacks" is also called "a signal", and there are several signal libraries for C++ around. I've used Boost.Signals before and it's nice and easy, although I've heard criticisms about its performance (which didn't matter much in the situations where I've used them).

##### Share on other sites

"A list of callbacks" is also called "a signal", and there are several signal libraries for C++ around. I've used Boost.Signals before and it's nice and easy, although I've heard criticisms about its performance (which didn't matter much in the situations where I've used them).

Thanks, I downloaded boost and got some wonky code working with signals. I'm sure I did it wrong because it won't let me put break points in the method it calls, but apparently it is running the code! So good enough for now at least.

##### Share on other sites
You are likely trying to debug in release mode. Which will often inline functions where possible.

##### Share on other sites

You are likely trying to debug in release mode. Which will often inline functions where possible.

Whoops, you're right I am in release mode. I switched to debug and I get an access violation in one of the boost header files when it tries to connect the signal though.

##### Share on other sites

Whoops, you're right I am in release mode. I switched to debug and I get an access violation in one of the boost header files when it tries to connect the signal though.

Check the call stack and inspect the variables in your program -- make sure you're hooking everything up correctly.

##### Share on other sites

[quote name='noodlyappendage' timestamp='1331615266' post='4921576']
Whoops, you're right I am in release mode. I switched to debug and I get an access violation in one of the boost header files when it tries to connect the signal though.

Check the call stack and inspect the variables in your program -- make sure you're hooking everything up correctly.
[/quote]

Hmm. It even happens in the basic Hello World example from the boost tutorial. I wonder if maybe it's some setting in the IDE? I'll keep investigating...

Edit: Looks like it is this http://stackoverflow.com/questions/137089/boostsignal-memory-access-error

##### Share on other sites
If you're using Visual Studio 2010 or newer (or a compiler that supports C++0x);

 bool CGame::Create( /* */ ) { tr1::function<void (CGame*)> CGameOnLoading = &CGame::OnLoading; m_nState = EGS_WORLD;//EGS_LOADING; CGameOnLoading( this ); return true; } 

Note that the tr1 namespace is inside the std namespace.

1. 1
2. 2
3. 3
JoeJ
20
4. 4
5. 5
frob
19

• 11
• 11
• 12
• 13
• 9
• ### Forum Statistics

• Total Topics
632213
• Total Posts
3004848

×