function pointer class member ?

S

sledge

Hi,
I am porting some legacy C code to C++. The code has a lot of member
function pointers like

class X {
int (*a)(int,int);
void (*b)(char t);
int x;
int f(int,int);
};

main()
{
// declare a pointer to member function
void (X::*a) (int) = &X::f;
This does not seem to work. What is the right syntax to handle the
function pointer?

Thanks
Sledge


}
 
C

cpp4ever

Hi,
I am porting some legacy C code to C++. The code has a lot of member
function pointers like

class X {
int (*a)(int,int);
void (*b)(char t);
int x;
int f(int,int);
};

main()
{
// declare a pointer to member function

void (X::*a)(int,int) = &X::f
Thanks
Sledge


}

The member function pointer was of the wrong type as you had the wrong
parameter list. See the solution above

HTH

cpp4ever
 
I

Ian Collins

Hi,
I am porting some legacy C code to C++. The code has a lot of member
function pointers like

class X {
int (*a)(int,int);
void (*b)(char t);
int x;
int f(int,int);
};

main()
{
// declare a pointer to member function
void (X::*a) (int) =&X::f;
This does not seem to work. What is the right syntax to handle the
function pointer?

You appear to be confusing a pointer to a member function and a member
that happens to be a function pointer!
 
G

Goran Pusic

Hi,
I am porting some legacy C code to C++.

If that is so, you should know first rule of porting, and that is:
"Don't". Porting is risky, and C++ is awesome enough that you can wrap
existing code (put a C++ façade on it) virtually without impacting
performance. Did you try that first?
The code has a lot of member
function pointers like

class X {
               int (*a)(int,int);
               void (*b)(char t);
                int x;
               int f(int,int);

};

main()
{
// declare a pointer to member function
 void (X::*a) (int) = &X::f;
This does not seem to work. What is the right syntax  to handle the
function pointer?

Obviously, this should be void (X::*a) (int, int) = &X::f; (note the
second "int").

On a more general note...

Your C function pointers do not seem to belong to X (logically, not
formally). If they "belonged", they would typically look like so:

int (*a)(X* this, int param1, int param2); (they would have "explicit"
this pointer.

That said, function pointers like these are normally a sign of an
attempt to get polymorphism in C (and if so, the above is not the best
version). If that is the purpose of the code, then you should just
switch to virtual functions.

Goran.
 
K

Kevin McCarty

Hi,
I am porting some legacy C code to C++. The code has a lot of member
function pointers like

class X {
               int (*a)(int,int);
               void (*b)(char t);
                int x;
               int f(int,int);

};

main()
{
// declare a pointer to member function
 void (X::*a) (int) = &X::f;
This does not seem to work. What is the right syntax  to handle the
function pointer?

This assignment is not possible (even after fixing the parameter list
of X::*a from 'int' to 'int, int'). X::f is a non-static member
function and X::a is a pointer to a free function. The two cannot be
made compatible.

- Kevin
 
P

Pavel

sledge said:
Hi,
I am porting some legacy C code to C++. The code has a lot of member
function pointers like

class X {
int (*a)(int,int);
void (*b)(char t);
int x;
int f(int,int);
};

main()
{
// declare a pointer to member function
void (X::*a) (int) =&X::f;
This does not seem to work. What is the right syntax to handle the
function pointer?

Thanks
Sledge


}
The following changed version of your example will compile although I am
not sure whether it's what you wanted (other people who mentioned X::a
class member does not seem to belong to the code were right: please note
that it is not related to the local variable `a' defined in main(); the
latter could be as well called `b' or anything without changing the
class X definition):

class X {
int (*a)(int,int);
void (*b)(char t);
int x;
public:
int f(int,int) { return 0; }
};

int main() {
int (X::*a) (int, int) = &X::f;
}

Hope this will help
-Pavel
 

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,955
Messages
2,570,117
Members
46,705
Latest member
v_darius

Latest Threads

Top