Hey i’m trying to make a Button template class, which is constructed with the the button would recieve when pressed (such as mouse position), and a pointer to the function that should be called.
However buttons often return void and take no arguments (Buttons that you press and something happens: they don’t take any arguments, they’re pressed and then just do something.) so how would i generate the classes member functions since apparently i can’t have void as an argument type?
Here’s the source if it’s helpful:
template<typename Return = void, typename Arg1 = void, typename Arg2 = void>
class Button
{
private:
boost::function<Return (Arg1, Arg2)> Function;
//Return (*Function)(Arg1, Arg2); // this didn't work so i tried boost::function
public:
void Activate(Arg1, Arg2){ Function(Arg1, Arg2) ;};
void SetFunction(Return (*Function)(Arg1, Arg2)){
this->Function= Function;};
//constructors
Button(){ Function= 0;};
Button( Return (*Function)(Arg1, Arg2)){
this->Function = &Function; };
};
You can specify a template specification of type void, for example, you could use the following variations of the templated class,
button:This then allows you to initialize and use void as arguments, for example, given a void function
Print()the following would now be valid:I hope this helps to clear things up! 🙂
Note:
nullptris a C++0x keyword, if your compiler hasn’t implemented it use#define nullptr 0