473,387 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1850
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: Joost Ronkes Agerbeek | last post by:
Why is it allowed in C++ to call a static member function of an object through an instance of that object? Is it just convenience? tia, Joost Ronkes Agerbeek
12
by: Steven T. Hatton | last post by:
This is something I've been looking at because it is central to a currently broken part of the KDevelop new application wizard. I'm not complaining about it being broken, It's a CVS images. ...
5
by: Eric Lilja | last post by:
Using a macro, can I change what type an object is being cast to? I know, the initial respone to question might be an instinctive "ugly, don't even think about it!" or "don't use macros at all",...
7
by: jon wayne | last post by:
Hi I'm a little confused here about the lifetime of a static pointer to member function, Say, I declare,define & initialize a static ptr to mem function in the header file of a class(the class...
5
by: RocTheEngy | last post by:
Greetings c.l.c... I am trying to understand some structure definitions in working code (e.g. compiles, runs & produces expected results) that I have. I think I've trimmed the code to what is...
7
by: Uwe Grawert | last post by:
Hello, a C-function expects me to give a pointer to a function as parameter. i want to give this function a pointer to a member function of a class but the compiler gives me an error: void...
4
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader =...
1
by: Bushido Hacks | last post by:
A private utility function using a function pointer sounds ideal to me. I want to simpify writing the same set of loops. While there are a few functions that can't use it, setting and modifying...
4
by: Immortal_Nephi | last post by:
I had a lot of research to see how function pointer works. Sometimes, programmers choose switch keyword and function in each case block can be called. Sometimes, they choose ordinary function...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.