help for function pointers

X

xuatla

Hi,

I have the following code for function pointer. compiling is ok. Can you
help me to check whether it's a good way to implement as:

class CA
{
.....
private:
void f1( double ) ;
void f2( double ) ;

private:
void (*CA::func)(double); // Q: is "CA::" here a must?

public:
void evalfunc( void (*f)(double) ) ; // Q: is this ok?
// I tried to use ( void (CA:*f)(double),
// but not good.

public:
void solve() ;
} ;

void CA::f1( double x ) { return 1.0; }

void CA::f2( double y ) { return 2.0; }

void CA::evalfunc( void (*f)(double) )
{
f(1); // ok.
.....
}

void CA::solve()
{
func = &CA::f1; // Q: if I dont use "CA::" or "&",
// then compile error.
// any other way for this part?

// above ok. but below not.
evalfun( func ); // error. Q: how to write this part?

// evalfunc( CA::func ); // error
}


Thanks a lot in advance for the help!

-xuatla
 
D

David White

xuatla said:
Hi,

I have the following code for function pointer. compiling is ok. Can you
help me to check whether it's a good way to implement as:

class CA
{
....
private:
void f1( double ) ;
void f2( double ) ;

private:
void (*CA::func)(double); // Q: is "CA::" here a must?

No, because the * is in the wrong place. Try this:
void (CA::*func)(double);
In the case, the CA:: is a must.
public:
void evalfunc( void (*f)(double) ) ; // Q: is this ok?

Not if you want 'f' to point to member f1 or f2.
Make it:
void evalfunc( void (CA::*f)(double) );
// I tried to use ( void (CA:*f)(double),
// but not good.

Well, you need two colons. You only have one. Use: void (CA::*f)(double)
public:
void solve() ;
} ;

void CA::f1( double x ) { return 1.0; }

void CA::f2( double y ) { return 2.0; }

You can't return doubles from void functions.
void CA::evalfunc( void (*f)(double) )
{
f(1); // ok.

OK, but what function is it calling? Not your f1 or f2.
Make this:
void CA::evalfunc( void (CA::*f)(double) )
{
(this->*f)(1);
}
.....
}

void CA::solve()
{
func = &CA::f1; // Q: if I dont use "CA::" or "&",
// then compile error.
// any other way for this part?

You should get a compile error no matter what for the definition of 'func'
that you have. For the new definition, this code is right.
// above ok. but below not.
evalfun( func ); // error. Q: how to write this part?

Just as it is, except you need to spell it right (evalfunc). The error
should go away now.
// evalfunc( CA::func ); // error

You don't need the CA:: here.

DW
 
V

Victor Bazarov

xuatla said:
I have the following code for function pointer. compiling is ok. Can you
help me to check whether it's a good way to implement as:

class CA
{
....
private:
void f1( double ) ;
void f2( double ) ;

private:
void (*CA::func)(double); // Q: is "CA::" here a must?

If you're trying to declare 'func' a pointer to a member function, it
should be

void (CA::*func)(double);
public:
void evalfunc( void (*f)(double) ) ; // Q: is this ok?

'f' here is a pointer to a function, not a pointer to a member function.
// I tried to use ( void (CA:*f)(double),
// but not good.

A single colon doesn't cut it. If you did post the code that was "not
good", we could help, hopefully.
public:
void solve() ;
} ;

void CA::f1( double x ) { return 1.0; }

void CA::f2( double y ) { return 2.0; }

void CA::evalfunc( void (*f)(double) )
{
f(1); // ok.
.....
}

void CA::solve()
{
func = &CA::f1; // Q: if I dont use "CA::" or "&",
// then compile error.
// any other way for this part?

Nope. That's the right syntax (provided 'func' is correctly
declared).
// above ok. but below not.
evalfun( func ); // error. Q: how to write this part?

Your 'evalfunc' has to accept a pointer to member function and inside
it has to invoke it correctly. See FAQ. There is a whole section on
pointers to members.
// evalfunc( CA::func ); // error
}

V
 
J

John Harrison

xuatla said:
Hi,

I have the following code for function pointer.

You must not get confused between function pointers, and member function
pointers.

void (*fp)();

fp is a function pointer, the function it points to is not a member of any
class.

void (X::*mfp)();

mfp is a member function pointer. The function it points to is a member of
the X class.

Function pointers and member function pointers are two completely separate
and incompatible things. It's a common newbie mistake to not realise that
there is any difference. Decide which version it is that you want and stick
with it.

There is one slight complication however, static member functions are
treated like non-member functions for the purposes of the above.

john
 
X

xuatla

Great thanks to you guys for the explanation and help.
I made some typo in my original post, but my problem is as you pointed
out. I am trying to correct it with the instruction from you guys. Thank
you.

-xuatla
 

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

Staff online

Members online

Forum statistics

Threads
474,183
Messages
2,570,968
Members
47,518
Latest member
TobiasAxf

Latest Threads

Top