943,791 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2211
  • C++ RSS
Aug 10th, 2009
0

Pointer to function returning a pointer to function

Expand Post »
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. }

C++ Syntax (Toggle Plain Text)
  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$
Similar Threads
Reputation Points: 25
Solved Threads: 0
Junior Poster in Training
ambarisha.kn is offline Offline
66 posts
since Jun 2008
Aug 10th, 2009
0

Re: Pointer to function returning a pointer to function

Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Aug 10th, 2009
0

Re: Pointer to function returning a pointer to function

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.
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Aug 10th, 2009
1

Re: Pointer to function returning a pointer to function

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++.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help with a counting program
Next Thread in C++ Forum Timeline: Bank program help!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC