Reputation: 113
I have a class with an array inside. Inside of this array, I want to put a function pointer to a function which has the same-this pointer as my insance.
class Foo {
std::vector <void (*baz)> bar;
void baz () {};
}
Foo bundy;
bundy.bar.push_back (/*???*/);
How can this be done? I suppose, when I run it like &Foo::baz()
, baz
has no (valid) this pointer or doesn't point to the instance.
What could be good workarounds?
Upvotes: 1
Views: 47
Reputation: 8386
Instead of storing a pointer to *baz
, you could store an std::function<void()>
.
Then you could store a lambda with a bound instance:
Foo bundy;
bundy.bar.push_back([&bundy](){ bundy.baz(); });
Your class would then look like this:
class Foo {
public:
std::vector<std::function<void()>> bar;
void baz() {};
};
Upvotes: 4