Created
September 30, 2022 14:20
-
-
Save metametaclass/9c0131c9e7dd19b67a137cdf98eda884 to your computer and use it in GitHub Desktop.
This file contains 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 <stdio.h> | |
#include <functional> | |
class Base; | |
// typedef void (*MainCallback)(void); | |
typedef std::function<void(void)> MainCallback; | |
class SubSub { | |
public: | |
SubSub() = default; | |
virtual ~SubSub() = default; | |
void setCallback(MainCallback val) { setParam = val; }; | |
void doIt() { | |
if (setParam) | |
setParam(); | |
}; | |
void run() { doIt(); }; | |
private: | |
MainCallback setParam; | |
}; | |
class Sub { | |
public: | |
Sub() = default; | |
virtual ~Sub() = default; | |
void setCallback(MainCallback val) { child.setCallback(val); }; | |
void run() { child.run(); }; | |
private: | |
SubSub child; | |
}; | |
class Base { | |
public: | |
Base() | |
{ | |
val = 7; | |
}; | |
Base(int i) { val = i; }; | |
virtual ~Base() = default; | |
void setCallback(MainCallback val) { sub.setCallback(val); }; | |
void init() { | |
setCallback(std::bind(&Base::OnCall, this)); | |
}; | |
void run() { sub.run(); }; | |
private: | |
Sub sub; | |
void OnCall() { printf("parent from child called!\n val=%i\n", val); }; | |
int val; | |
}; | |
int main() { | |
Base foo, bar; | |
foo.init(); | |
foo.run(); | |
bar.init(); | |
bar.run(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment