开发者

What is the typical use for operator() [duplicate]

Th开发者_开发知识库is question already has answers here: Closed 11 years ago.

Possible Duplicate:

How can overloading operator “function call” in C++ be useful?

I see often where the parenthesis operator, operator(), is overloaded on a class or struct. I have never run into such a need myself, and wonder what is the typical use/need of this operator?

For instance, overloading operator== is accepted as returning true or false based on some equality made with the argument provided. This has a certain accepted and anticipated behavior.


It's used a lot for the hashes, sets and algorithms component of STL to construct things called 'functors'. For example:

using stl::map ;

map<string, string> myMap ;

but if you look at the specification for map it also includes a 'Compare' entry which defaults to less. The Compare is the 'functor' you want map to use when comparing two keys. It looks something like this:

template<class Key>
class less<Key> {
public:
  boolean operator()(const Key& a, const Key& b) { return a < b ;}
} ;

so what you have is:

 map<string, string, less<string> > myMap ;

and evertime you put something in the map or try to find something in the map it will use the less::operator() to do the comparisons.

You can use this to acchieve other effects such as when using 'sort' you can get sort to yield items in reverse order by using a different functor.


You can use operator(), also known as the 'function call operator', to make an object behave like (and be usable as) a function.

These objects are often referred to as 'functors', but that shouldn't be confused with the same term from mathematics. I believe a more proper term is 'function object'.

Imagine you have a function that takes a callback. But suppose you need to store some state in that callback.

[note: I haven't compiled this code, but you get the idea]

void DoStuffAndCallBack(MyCallbackType Callback)
{
    ...
    Callback(args)
    ...
}

In C, or in 'C-with-classes'-style C++, you would pass a static callback function to this method, and store results in some global or class-static variable.

But in C++, the function could be written like so:

template<CallbackType>
void DoStuffAndCallBack(CallbackType Callback)
{
    ...
    Callback(args)
    ...
}

Now you can define a functor object that overloads operator() and pass an instance of that object to the function. That instance will get the callback, and any intermediate results you want to store can be stored in that instance.

In other words, it's a nice way to avoid globals (:

I'm sure there are other uses, but that's one good one, IMO.


The canonical example is for a smart pointer class returning whether or not it is null, so you can do things like:

SmartPointer p = someOtherThing;
if (p) 
{ 
// do something 
}

In this case, the return type of the () operator should be bool, and the code will work as written. It creates a functor that allows the instance of the class to be called like a function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜