Pointer to function returning a pointer to function

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2008
Posts: 66
Reputation: ambarisha.kn is an unknown quantity at this point 
Solved Threads: 0
ambarisha.kn ambarisha.kn is offline Offline
Junior Poster in Training

Pointer to function returning a pointer to function

 
0
  #1
Aug 10th, 2009
Question: Declare a pointer to a function taking an int argument and returning a pointer to a function that takes a char argument and returns a float.

I did code as follows. But it is giving error. Please correct this.

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. float func(int x)(char c) {
  5. cout << "func() called..." << endl;
  6. }
  7.  
  8.  
  9. int main() {
  10. int x = 2;
  11. char c;
  12. float (*(*fp)(int))(char); // Define a function pointer
  13. (*fp) = func; // Initialize it
  14. (*(*fp))(x)(c); // Dereferencing calls the function
  15. }

  1. ambarish@ubuntu:~/exercise$ g++ ex33.cpp
  2. ex33.cpp:4: error: ‘func’ declared as function returning a function
  3. ex33.cpp: In function ‘int main()’:
  4. ex33.cpp:13: error: assignment of read-only location ‘* fp’
  5. ex33.cpp:13: error: cannot convert ‘int ()(int)’ to ‘float (* ()(int))(char)’ in assignment
  6. ambarish@ubuntu:~/exercise$
Ambarish
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,562
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 454
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Pointer to function returning a pointer to function

 
0
  #2
Aug 10th, 2009
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Pointer to function returning a pointer to function

 
0
  #3
Aug 10th, 2009
Something like this?

  1. typedef float (*psraBProc)( int x );
  2.  
  3. float psraBFunc( int x )
  4. {
  5. }
  6.  
  7. int r;
  8. psraBProc psraB;
  9. float f;
  10.  
  11. psraB = psraBFunc;
  12. f = psraB( r );
Last edited by wildgoose; Aug 10th, 2009 at 12:56 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Pointer to function returning a pointer to function

 
1
  #4
Aug 10th, 2009
I suggest you take a read through The Function Pointer Tutorials, particularly this one.

I would use some typedefs to help:
  1. typedef float (char2float_t)( char );
  2. typedef char2float_t* int2char2float_t( int );
  3.  
  4. ...
  5.  
  6. char2float_t* wonky_function_1( int n )
  7. {
  8. ...
  9. }
  10.  
  11. ...
  12.  
  13. int2char2float_t* fp1 = &wonky_function_1;
  14.  
  15. char2float_t* fp2 = fp1( 42 ); /* or: fp2 = (*fp1)( 42 ); */
  16. float value = fp2( 'A' ); /* likewise: value = (*fp2)( 'A' ); */
BTW, you really are doing something weird. What goal can you possibly be trying to accomplish? (It appears to me that there is a design flaw in your program.) Perhaps we can suggest something better (and easier).

Also, this is the C forum. You are using C++.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC