dcsimg
Login | Register   
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


advertisement
 

Use Function Adapters to Extend Generic Algorithms' Usage-4 : Page 4


advertisement

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


Further Adaptation
Containers of pointers are used, among other things, for implementing of a heterogeneous container:

class Base { public: virtual void speak() const {std::cout<<"I'm Base"<<std::endl;} }; class Derived : pubic Base { public: void speak()const {std::cout<<"I'm Derived"<<std::endl;} }; std::vector<Base *> vpb; //heterogeneous vpb.push_back(new Base); vpb.push_back(new Derived); vpb.push_back(new Base);

Suppose you want to call speak() for every element of vpb. Again, using for_each() is a preferable choice. However, because vpb stores pointers rather than objects, you have to use a different adapter this time, namely mem_fun():

 
Figure 3: This shows the output from this for_each() call:.



std::for_each (vpb.begin(), vpb.end(), std::mem_fun(&Base::speak));

Figure 3 shows the output of this for_each() call.

Summary
The Standard Library defines a rich set of adapters:

  • ptr_fun takes a function pointer as its argument and returns a function pointer adaptor, which is a kind of a function object.
  • The mem_fun_ref family binds a member function to an object's reference and returns a function object.
  • The mem_fun functions binds a member function to an object's pointer and returns a matching function object.
Each adapter is classified according to these criteria: does it apply to an ordinary function or a member function? If it's the latter, the adapter functions contain the affix mem_. If the adapter binds a member function to a reference rather than a pointer, it contains the affix ref_. Finally, if the member function takes an argument, then the adapter contains the affix fun1_ instead of fun_. For example mem_fun1_ref() binds a member function taking one argument to a reference, whereas mem_fun1() binds a member function taking one argument to a pointer.



Danny Kalev is a system analyst and software engineer with 13 years of experience, specializing in C++ and object-oriented analysis and design. He is a member of the ANSI C++ standardization committee and the author of ANSI/ISO C++ Professional Programmer's Handbook (Que, 1999, ISBN: 0789720221). Reach him at dannykk@inter.net.il.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date