pointer to function

G

Gernot Frisch

static short gfunc(short a, short b)
{return 0;}

class B{
B();
short(m_func)(short, short);
}

B::B()
{
m_func = gfunc;
}


error C2659: '=' : overloaded function as left operand

I'm not very familiar with pointer to functions. Can you help?

Thank you,
Gernot

--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
 
S

Senapathy

Gernot Frisch said:
static short gfunc(short a, short b)
{return 0;}

// 1. Create a type for the function pointer...

typedef short(*pfn)(short, short);
class B{
B();
short(m_func)(short, short);

// 2. Have a member variable of that type

pfn m_func;
}

B::B()
{
m_func = gfunc;
}

Rest should be fine:

// invoke the function
B bObj;
bObj.m_func(1, 2);

Regards,
Senapathy
 
S

Senapathy

Senapathy said:
// 1. Create a type for the function pointer...

typedef short(*pfn)(short, short);

Ok, the type creation is not really required.... I just used it the way I am
always used to creating fn ptrs.

Just change this to short( * m_func)(short, short);
^^^^
After all you want to declare a function _pointer_ !

Regards,
Senapathy
 
G

Gernot Frisch

Just change this to short( * m_func)(short, short);
^^^^
After all you want to declare a function _pointer_ !

Ah, all right. What good would "void (a)()" be for? Would it mean the
same as "void a();" ?
Anyway, thank you.
Gernot
 
J

JKop

Gernot Frisch posted:
Ah, all right. What good would "void (a)()" be for? Would it mean the
same as "void a();" ?
Anyway, thank you.
Gernot


Yes, it would.

int (main)(void)
{

}


-JKOp
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top