Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


#ActualAressera

Posted 15 February 2013 - 12:32 AM

For my GUI system I tend to prefer function objects (i.e. Function<void ()>), I guess there's boost::function and now std::function to choose from, though I use my own implementation.

 

This allows you to do stuff like:

 

class ButtonDelegate
{
	public:
	
	Function<void ( Button& )> press;
	Function<void ( Button& )> release;
}

void press( Button& button )
{
}

void release( Button& button )
{
}

void main()
{
	Button* b = new Button();
	
	ButtonDelegate delegate;
	delegate.press = press;
	delegate.release = release;
	
	b->setDelegate( delegate );
}

 

 

They might not be the fastest (due to the extra indirection), but they're definitely the most flexible way to do things in a static language like C++. You could also just use regular function pointers, but this would make it trickier to have class instance methods receive events.

 

You are probably prematurely optimizing here, you should design your system for optimum usability before worrying about the overhead for a function call. It's probably not going to be a performance bottleneck anyway unless you are sending hundreds of thousands of events per second.


#2Aressera

Posted 15 February 2013 - 12:31 AM

For my GUI system I tend to prefer function objects (i.e. Function<void ()>), I guess there's boost::function and now std::function to choose from, though I use my own implementation.

 

This allows you to do stuff like:

 

class ButtonDelegate
{
	public:
	
	Function<void ( Button& )> press;
	Function<void ( Button& )> release;
}

void press( Button& button )
{
}

void release( Button& button )
{
}

void main()
{
	Button* b = new Button();
	
	ButtonDelegate delegate;
	delegate.press = press;
	delegate.release = release;
	
	b->setDelegate( delegate );
}

 

 

They might not be the fastest (due to the extra indirection), but they're definitely the most flexible way to do things in a static language like C++. You could also just use regular function pointers, but this would make it trickier to have instance methods receive events.

 

You are probably prematurely optimizing here, you should design your system for optimum usability before worrying about the overhead for a function call. It's probably not going to be a performance bottleneck anyway unless you are sending hundreds of thousands of events per second.


#1Aressera

Posted 15 February 2013 - 12:31 AM

For my GUI system I tend to prefer function objects (i.e. Function<void ()>), I guess there's boost::function and now std::function to choose from, though I use my own implementation.

 

This allows you to do stuff like:

 

class ButtonDelegate
{
	public:
	
	Function<void ( Button& )> press;
	Function<void ( Button& )> release;
}

void press()
{
}

void release()
{
}

void main()
{
	Button* b = new Button();
	
	ButtonDelegate delegate;
	delegate.press = press;
	delegate.release = release;
	
	b->setDelegate( delegate );
}

 

 

They might not be the fastest (due to the extra indirection), but they're definitely the most flexible way to do things in a static language like C++. You could also just use regular function pointers, but this would make it trickier to have instance methods receive events.

 

You are probably prematurely optimizing here, you should design your system for optimum usability before worrying about the overhead for a function call. It's probably not going to be a performance bottleneck anyway unless you are sending hundreds of thousands of events per second.


PARTNERS