GeeksforGeeks » C/C++ Programming Questions
Output of C++ program?
(2 posts)-
#include<iostream> int f() { return 2; } class temp { public: int operator()() { return 1; } }; int main() { temp f; std::cout<<f(); getchar(); return 0; } -
Answer 1.
It is due to the overloading "conversion function" 'operator()()'. Conversion functions (functor) are useful for passing parameters and returning appropriate type.
Another form is 'conversion operator'
syntax ----- operator type-name () {}
Conversion operator converts the user defined concrete type into required type. Example,
class RollCommand
{
public:
// Some processing functionsoperator const double ()
{
return rollAngle;
}
private:
double rollAngle;
};RollCommand rc;
...
// Backs up the previous roll angle
backUp = rc;The return type need not necessarily the primitive type. It can be another user defined type. The functor will do necessary conversions prior to returning the intended object.
Reply
You must log in to post.