469,342 Members | 5,699 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,342 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?

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
3 1648
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
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
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.

Similar topics

30 posts views Thread by Joost Ronkes Agerbeek | last post: by
12 posts views Thread by Steven T. Hatton | last post: by
7 posts views Thread by jon wayne | last post: by
5 posts views Thread by RocTheEngy | last post: by
7 posts views Thread by Uwe Grawert | last post: by
4 posts views Thread by grizggg | last post: by
1 post views Thread by Bushido Hacks | last post: by
4 posts views Thread by Immortal_Nephi | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by haryvincent176 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.