C++11 - a good example of using std::function
less than 1 minute read
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
struct Wanderer
{
explicit Wanderer ( std ::vector < std ::function >& update _ loop )
{
update _ loop . emplace _ back ([ this ]( float dt ) { update ( dt ) ; }) ;
}
void update ( float dt ) ;
} ;
struct Diver
{
explicit Diver ( std ::vector < std ::function >& update _ loop )
{
update _ loop . emplace _ back ([ this ]( float dt ) { update ( dt ) ; }) ;
}
void update ( float dt ) ;
} ;
int main ()
{
std ::vector < std ::function > update _ loop ;
Wanderer wanderer { update _ loop } ;
Diver diver { update _ loop } ;
while ( true )
{
for ( auto & function : update _ loop )
{
function ( 0.016 f ) ;
}
}
}