Skip to content

Instantly share code, notes, and snippets.

@metametaclass
Created September 30, 2022 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save metametaclass/9c0131c9e7dd19b67a137cdf98eda884 to your computer and use it in GitHub Desktop.
Save metametaclass/9c0131c9e7dd19b67a137cdf98eda884 to your computer and use it in GitHub Desktop.
#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