Shortcomings of passing functions?

I am learning C ++ (from java) and recently discovered that you can pass functions. This is really great and I think it is very useful. Now I was thinking about how I can use this, and one of the ideas that came to my mind was a fully customizable class.

The best example of my train, although for fully customizable classes (code), could be called a human class. The person will have all the functions related to P. Later, the person can take the sword (S), so now the person has access to all functions related to both P and S.

Are there any limitations or performance issues? Is it careless and just frowned?

Any insight is educational, thanks.

~ aedona

+5
source share
3 answers

There is a slight performance hit because the pointer or reference must be dereferenced before the function is called.

This is a very profitable feature. Many design patterns and polymorphism depend on function pointers. Check out the Visitor Design Template.

Another use for function table. For example, you can write a general menu mechanism that displays various menus using various functions.

Also explore the Factory design template.

+2
source

- - , , , (, , , ).

STL . : , operator(), . , , operator() (, std::less<T>).

: http://www.sgi.com/tech/stl/functors.html

+4

, . , , , . . , ++!

, ++ , . , operator(), , . : , . std::sort, , C qsort, qsort , std::sort .

+2

All Articles