0
#include <iostream>
void hello1() {
    std::cout << "Hello from normal\n";
}
auto hello2 = [] {
    std::cout << "Hello from lamda\n";
};
class hello
{
public:
    hello() {}
    void operator()(std::string str) {
        std::cout << "From functor " << str << std::endl<<std::endl;
    }
};
void doIt(void(*f)()) {
    f();
}
int main() {
    hello1();
    hello2();
    hello hello3;
    hello3("Hello");    
    doIt(hello1);
    doIt(hello2);
    doIt(hello3); //How can I do this?
}

I just started learning function objects and I cannot call function object from doIt function How can I call the last function object with doIt function?

Any help would be appreciated.

  • 2
    Why is the call to `doIt(hello3)` coded differently than the other two? – chris Apr 22 '20 at 03:40
  • Please add the exact error message you get for the line that's not working. – cigien Apr 22 '20 at 03:45
  • 1
    if [How to assign functor to function pointer?](https://stackoverflow.com/questions/23795348/how-to-assign-functor-to-function-pointer) isn't a duplicate, it's really close. – user4581301 Apr 22 '20 at 03:59
  • Here's a better related answer: [passing functor as function pointer](https://stackoverflow.com/questions/1840029/passing-functor-as-function-pointer) – user4581301 Apr 22 '20 at 04:12

0 Answers0