Skip to content

Instantly share code, notes, and snippets.

@farnoy
Created December 9, 2010 11:48
Show Gist options
  • Save farnoy/734640 to your computer and use it in GitHub Desktop.
Save farnoy/734640 to your computer and use it in GitHub Desktop.
Dynamic function calling with C++
class Class
{
public:
int a (int a, short b)
{
return a + b;
}
int (Class::*PointerToA())(int, short)
{
return &Class::a;
}
};
int function(int a, short b)
{
return a + b;
}
#include <iostream>
#include "function.hpp"
#include "class.hpp"
int main(int argc, char** argv)
{
int (*p2func) (int, short) = NULL;
p2func = &function;
Class* instance = new Class;
std::cout << "p2func(5, 8);" << std::endl << p2func(5, 8) << std::endl;
std::cout << "(instance->*(instance->PointerToA()))(5, 8);" << std::endl << (instance->*(instance->PointerToA()))(5, 8) << std::endl;
delete instance;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment