By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,938 Members | 1,766 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,938 IT Pros & Developers. It's quick & easy.

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

P: n/a
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;
}

Jul 6 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
za******@gmail.com wrote:
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?
http://www.parashift.com/c++-faq-lit....html#faq-33.5

--
rbh
Jul 6 '07 #2

P: n/a
On Fri, 06 Jul 2007 05:00:27 -0700, zaeminkr wrote:
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;
}
--
Lionel B
Jul 6 '07 #3

P: n/a
On 7 6 , 9 17 , Robert Bauck Hamar <roberth+n...@ifi.uio.nowrote:
zaemi...@gmail.com wrote:
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?

http://www.parashift.com/c++-faq-lit....html#faq-33.5

--
rbh
Thanks but I already found that and it does not work.

Jul 6 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.