Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing around a nested functor (C++)

Tags:

c++

functor

Is there a way to pass foo_ around outside of main? I saw something about Boost in another question regarding functors. That looks like it may work. Here's the answer mentioning Boost in that question. If I can, I would like to avoid Boost.

#include <iostream>

int main()
{
    class foo {
        public:
        void operator()() {
            std::cout << "Hello" << std::endl;
        }
    };

    foo foo_;
    foo_();
    return 0;
}
like image 458
Scott Avatar asked Jun 18 '26 16:06

Scott


1 Answers

No, currently local types aren't allowed to go into templates (otherwise you could've used boost or std::tr1::function). However, you could maybe do it OOP, where Foo inherits something (that has a virtual opeator() func that your foo implemen ts) and you pass a ptr to Foo around instead.

like image 179
Macke Avatar answered Jun 21 '26 06:06

Macke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!