c语言回调函数参数,c – 传递回调函数的参数

如果函数没有将参数用于回调以外的任何内容,我会完全删除回调:

// c++0x, same can be achieved with boost::function/boost::bind in c++03

void function( std::function< void ( void ) > f )

{

f();

}

void callback1( int );

void callback2( Type );

//void callback3( double );

void user_code() {

function( std::bind( callback1, 5 ) );

function( std::bind( callback2, Type( 1, 2, 3, 4 ) );

// function( std::bind( callback3, 5.0 );

}

通过使用只从函数内部传递参数(none)的泛型函子(std :: function),可以将函数与回调分离.现在你可以传递任何类型,并且由调用者来绑定回调值(即回调参数不是函数的责任,函数也不需要知道它的类型或值).