473,473 Members | 2,021 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

help for function pointers

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
Jul 22 '05 #1
5 1479
"xuatla" <xu****@gmail.com> wrote in message
news:41**************@gmail.com...
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

Jul 22 '05 #2
"xuatla" <xu****@gmail.com> wrote...
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
Jul 22 '05 #3

"xuatla" <xu****@gmail.com> wrote in message
news:41**************@gmail.com...
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
Jul 22 '05 #4

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

John Harrison wrote:
"xuatla" <xu****@gmail.com> wrote in message
news:41**************@gmail.com...
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

Jul 22 '05 #5

I corrected my code in these ways and it works now. Thanks a lot!

-xuatla

David White wrote:
"xuatla" <xu****@gmail.com> wrote in message
news:41**************@gmail.com...
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

Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: CoolPint | last post by:
I would be grateful if someone could point out if I am understanding correctly and suggest ways to improve. Sorry for the long message and I hope you will kindly bear with it. I have to make it...
5
by: Jeff Greenberg | last post by:
Not an experienced c++ programmer here and I've gotten myself a bit stuck. I'm trying to implement a class lib and I've run into a sticky problem that I can't solve. I'd appreciate any help that I...
2
by: leo2100 | last post by:
Hi, I need help with this program. The program is supposed to take a text file and identify the words in it, then it should print them and count how many times a word is repeated. At first main...
1
by: ArcInversion | last post by:
Hi, I've been using a javascript script to create a dragon that flies across the page. Anyways, I'd like to make it so when you click the dragon it takes you to a new page. Was wondering if anyone...
4
by: Christian Maier | last post by:
Hi After surfing a while I have still trouble with this array thing. I have the following function and recive a Segmentation fault, how must I code this right?? Thanks Christian Maier
6
by: M Turo | last post by:
Hi, I was wondering if anyone can help. I'm want to pre-load a 'table' of function pointers that I can call using a its arrayed index, eg (non c code example) pFunc = func_A; pFunc = func_B;
3
by: googlinggoogler | last post by:
Hi This should all be pretty standard C stuff, but I'm going to use terms like mouse callback to communicate what Im tyring to do. Basically I have my program whirling around in an infinite...
2
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.