pointer to member functions

Though with a similiar grammar as that of free funstions and static class member functions,  pointers to member functions  are not addresses, but offsets – how can the addresses be determined when the class is not initialised on the stack/heap yet?

Below are two ways to  define and use member function pointers:

  1: // C-style version
  2: void shrink(raw_path_groups_type rpgs)
  3: {
  4:     // define the pointer
  5:     typedef detectiveT<walkerT, equal_predicateT> this_class;
  6:     typedef void (this_class::*func_type)(raw_path_groups_type); 
  7: 
  8:     boost::array<func_type, 5> f = 
  9:     {
 10:         &this_class::single_threaded_process, 
 11:         &this_class::concurrent_process_thread_group,
 12:         &this_class::concurrent_process_ppl,
 13:         &this_class::concurrent_process_tbb,
 14:         &this_class::concurrent_boost_task   
 15:     };  
 16:        
 17:     (this->*f[m_thread_mode])(rpgs); // invoke it
 18:     //alternatively, boost::bind(f[m_thread_mode], this, rpgs)();    
 19: }

alternatively, below is a boost::function version.

  1: // boost::function-style version
  2: void shrink(raw_path_groups_type rpgs)
  3: {
  4:     typedef detectiveT<walkerT, equal_predicateT> this_class;
  5:     typedef boost::function<void(this_class*, raw_path_groups_type)> funct_type;
  6:     
  7:     boost::array<funct_type, 5> f = 
  8:     {
  9:         // boost::mem_fn is necessary for VC++10 to resolve ambiguity with tr1;
 10:         // but necessary not for VC9 
 11:         boost::mem_fn(&this_class::single_threaded_process), 
 12:         boost::mem_fn(&this_class::concurrent_process_thread_group),
 13:         boost::mem_fn(&this_class::concurrent_process_ppl),
 14:         boost::mem_fn(&this_class::concurrent_process_tbb),
 15:         boost::mem_fn(&this_class::concurrent_boost_task)   
 16:     }; 
 17:     
 18:     f[m_thread_mode](this, rpgs); 
 19:     //alternatively, boost::bind(f[m_thread_mode], this, rpgs)();                
 20: } 

Advertisements
Post a comment or leave a trackback: Trackback URL.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: