Skip to content

Instantly share code, notes, and snippets.

@Gerjo
Created October 23, 2012 13:53
Show Gist options
  • Save Gerjo/3938867 to your computer and use it in GitHub Desktop.
Save Gerjo/3938867 to your computer and use it in GitHub Desktop.
Pointer to member.
#include <cstdlib>
#include <iostream>
using namespace std;
class Bar {
public:
Bar(std::string thoughts) : _thoughts(thoughts) {
}
void foo(void) {
cout << "Foolicious: " << _thoughts << endl;
}
private:
std::string _thoughts;
};
int main(int argc, char** argv) {
// Pointer to method:
void (Bar::* method) (void) = &Bar::foo;
// Works for stack:
Bar bar("Quite indeed.");
(bar.*method)();
// Works for heap:
Bar* barPointer = new Bar("You are wrong.");
(barPointer->*method)();
// Now we've got that covered: How about using the auto keyword?
auto method2 = &Bar::foo;
(bar.*method2)();
// No memory leakage in this example.
delete barPointer;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment