doubt in - Function Objects - c++
- by Eternal Learner
I have a class
class fobj{
public:
fobj(int i):id(i) {}
void operator()()
{
std::cout<<"Prints"<<std::endl;
}
private:
int id;
};
template<typename T>
void func(T type)
{
type();
}
My Doubt is if I invoke func like
Method 1:
func(fobj(1);
the message I wanted to print is printed.
I was always thinking I needed to do something like
Method 2:
fobj Iobj(1); // create an instance of the fobj class
func(Iobj); //call func by passing Iobj(which is a function object)
How does Method 1 work? I mean what exactly happens? and how is a call made to the operator() in class fobj ?