Created
April 10, 2021 05:05
-
-
Save The-EDev/740d85d23da2e30480b5a5fc9357b999 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <functional> | |
| #include <iostream> | |
| class B | |
| { | |
| public: | |
| template <typename Func> | |
| void operator()(Func&& f) | |
| { | |
| handler_ = ([f](int a, int b){f();}); | |
| std::cout << std::boolalpha << handler_exists() << std::endl; | |
| std::cout << this << std::endl; | |
| } | |
| bool handler_exists() | |
| { | |
| return (handler_ != nullptr); | |
| } | |
| protected: | |
| friend class C; | |
| private: | |
| std::function<void(int& a, int& b)> handler_; | |
| }; | |
| class C | |
| { | |
| public: | |
| int a; | |
| int b; | |
| B* get_b() | |
| { | |
| bthing = new B(); | |
| std::cout << bthing << std::endl; | |
| return bthing; | |
| }; | |
| void handle() | |
| { | |
| std::cout << std::boolalpha << bthing->handler_exists() << std::endl; | |
| std::cout << bthing << std::endl; | |
| bthing->handler_(a, b); | |
| } | |
| private: | |
| B* bthing; | |
| }; | |
| class A | |
| { | |
| public: | |
| auto bc() | |
| { | |
| return cthing.get_b(); | |
| } | |
| void handle() | |
| { | |
| cthing.handle(); | |
| } | |
| private: | |
| C cthing; | |
| }; | |
| int main() | |
| { | |
| A a_item; | |
| (*(a_item.bc()))([](){std::cout << "it works" << std::endl;}); | |
| a_item.handle(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment