how call a method through an function pointer?

  • Thread starter dennis luehring
  • Start date
D

dennis luehring

i try to "publish" some of my very stupid methods (i need this for an interpreter)

function_one
procedure_one

here is the code

---------
class core
{
public:
int result;
int parameter[2];

typedef void(core::*call)();
typedef struct method_t
{
bool function; // additional information
call method; // the call
};

method_t methods[2];

core()
{
methods[0].function = true;
methods[0].method = function_one;

methods[1].function = false;
methods[1].method = procedure_one;
}

// callable methods
void function_one(){ result = parameter[0] + parameter[1]; }
void procedure_one(){ parameter[0] = 10; }
};

void main()
{
core c;

// why can't i call the method...
//c.methods[0].method();
// can't compile this - error
// error C2064: term does not evaluate to a function taking 0 arguments

// ...should be the same as
c.function_one();
}
 
J

Jason

dennis said:
---------
class core
{
public:
int result;
int parameter[2];

typedef void(core::*call)();
typedef struct method_t
{
bool function; // additional information
call method; // the call
};

method_t methods[2];

core()
{
methods[0].function = true;
methods[0].method = function_one;

methods[1].function = false;
methods[1].method = procedure_one;
}

// callable methods
void function_one(){ result = parameter[0] + parameter[1]; }
void procedure_one(){ parameter[0] = 10; }
};

void main()
{
core c;

// why can't i call the method...
//c.methods[0].method();
// can't compile this - error
// error C2064: term does not evaluate to a function taking 0 arguments

// ...should be the same as
c.function_one();
}

Pointers to member functions are in the FAQ, but I can't help but think
that there must be a better way... Maybe polymorphism? What are you
trying to achieve?

http://www.parashift.com/c++-faq-lite/pointers-to-members.html

-Jason
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top