What is the correct grammar to make a function call by using static member data which is a pointer t

Z

zaeminkr

I have class member data which is a pointer to class member function.

However, I'm fail to compile the code.

What is the correct grammar to make a function call by using static
member data which is a pointer to a member function?


class Test
{
public :
typedef int (Test::*pfnOperate)(int x, int y);

static const pfnOperate m_arpOperate[2];

public :
int Add(int x, int y);
int Subtract(int x, int y);
void Foo();
};

const Test::pfnOperate Test::m_arpOperate[2] = {Test::Add,
Test::Subtract};

int Test::Add(int x, int y)
{
return x + y;
}

int Test::Subtract(int x, int y)
{
return x - y;
}

void Test::Foo()
{
int result;

result = (Test::m_arpOperate[1])(10, 5); // error C2064: term does
not evaluate to a function
}

int main(int argc, char* argv[])
{
Test t;

t.Foo();
return 0;
}
 
L

Lionel B

I have class member data which is a pointer to class member function.

However, I'm fail to compile the code.

What is the correct grammar to make a function call by using static
member data which is a pointer to a member function?


class Test
{
public :
typedef int (Test::*pfnOperate)(int x, int y);

static const pfnOperate m_arpOperate[2];

public :
int Add(int x, int y);
int Subtract(int x, int y);
void Foo();
};

const Test::pfnOperate Test::m_arpOperate[2] = {Test::Add, Test::Subtract};

g++ insists I write:

const Test::pfnOperate Test::m_arpOperate[2] = {&Test::Add, &Test::Subtract};

I'm not clear on whether the standard actually requires this.
int Test::Add(int x, int y)
{
return x + y;
}

int Test::Subtract(int x, int y)
{
return x - y;
}

void Test::Foo()
{
int result;

result = (Test::m_arpOperate[1])(10, 5); // error C2064: term does not evaluate to a function


result = (this->*m_arpOperate[1])(10, 5);
}

int main(int argc, char* argv[])
{
Test t;

t.Foo();
return 0;
}
 

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

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top