Sign in to follow this  

Trouble With Syntax: Function Pointers

This topic is 4862 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I was wondering the correct syntax for instancating, passing and using a function pointer that is passed no arguments and returns a pointer. Something to the effect of: void DoSomething( char *(*pfFoo)() ){ char *(*)() pfWidget = pfFoo; char *pCh; pCh = pfWidget(); } Or perhaps using a typedef? I have seen two forms mentioned. typedef char*(*pfFoo)(); pfFoo pfWidget; /*instanceating the typedef*/ typedef pfFoo char*(*)(); pfFoo pfWidget; /*instanceating the typedef*/ Also, I assume the syntax could be that same for a user defined data type. I must apologize for my inability to spell, but i would appreciate any help you are willing to offer. -Boog

Share this post


Link to post
Share on other sites
Quote:
Original post by Boog
Hello,

I was wondering the correct syntax for instancating, passing and using a function pointer that is passed no arguments and returns a pointer. Something to the effect of:

void DoSomething( char *(*pfFoo)() ){
char *(*)() pfWidget = pfFoo;
char *pCh;

pCh = pfWidget();
}


Or perhaps using a typedef? I have seen two forms mentioned.

typedef char*(*pfFoo)();
pfFoo pfWidget; /*instanceating the typedef*/


Yeah thats wright, i would use a typedef, saves you from some typing and makes the code look clear:


#include <iostream>

typedef const char* const const_char_ptr;

typedef const_char_ptr (*pfunc)();

void DoSomething(pfunc f) {

std::cout << f() << '\n';

}

const_char_ptr func1() {
static const_char_ptr f = "HELLO";

return f;

}

const_char_ptr func2() {
static const_char_ptr f = "GOODBYE";

return f;

}

int main() {

DoSomething(func1);

DoSomething(func2);

return 0;

}







Quote:
Original post by Boog
Also, I assume the syntax could be that same for a user defined data type.


If you mean member function/data pointers it's not quite the same they really become offsets that are complete when used with an instance of the type in question so they don't do much on there own, you can't mix the two there not the same e.g.


#include <iostream>

struct b {

void do_it1() const { std::cout << "HELLO\n"; }

void do_it2() const { std::cout << "GOODBYE\n"; }

};

typedef void (b::*PMF)() const;

int main() {

PMF f1 = &b::do_it1;
PMF f2 = &b::do_it2;

b test;

//direct calls
test.do_it1();
test.do_it2();

//indirect calls

(test.*f1)();
(test.*f2)();

// or
const b* const test_ptr = &test;

(test_ptr->*f1)();
(test_ptr->*f2)();

return 0;

}





Share this post


Link to post
Share on other sites
Typedefs make the code much easier to read. The syntax for typedef is the same as a declaration except the name of the type is used instead of the name of the variable.
In the case of a function pointer, (*pfFoo) replaces the name of the function, where pfFoo is the name of the variable (in a declaration) or the type (in a typedef).
Quote:
Original post by Boog
typedef char*(*pfFoo)();
pfFoo pfWidget; /*instanceating the typedef*/
This is a pointer to a function with no parameters that returns a char pointer.
Quote:
Original post by Boog
typedef pfFoo char*(*)();
pfFoo pfWidget; /*instanceating the typedef*/

This won't compile.

Share this post


Link to post
Share on other sites
If you have a function:
char Func ( int, float );

You can create a pointer to it like this:
char (*Ptr) (int,float);
Ptr = Func;
C = Ptr(A, B); // this calls Func(A,B)

Or, using a typedef, you can do like you said:
typedef char (*FuncPtr) (int,float);
FuncPtr Ptr = Func;
C = Ptr(A, B);

To use a pointer to a function that's a method of class TestClass, you do this:
typedef char (TestClass::*TestFuncPtr) (int, float);

Unfortunately I can't remember the code for calling this function. It's pretty ugly-looking, so I wrote a macro for it, but I'd done it a long time ago and can't find that file at the moment.

Share this post


Link to post
Share on other sites

This topic is 4862 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this