Thread: Problem with a function pointer syntax

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

    Problem with a function pointer syntax

    Look at the code:
    Code:
    class X
    {
    public:
      int foo(int,int);
      static int bar(/*ARGS*/);  
    };
    I want the arguments of bar to be one or more function pointers that can only point to foo of any object of class X.
    What would be the correct syntax for that ?
    Also, what is the typename of a function pointer written as, for ...say...templates ?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You probably want to look at this:

    http://www.parashift.com/c++-faq-lit....html#faq-33.9

    Which, altho it is very informative, there are, unfortunately, no complete working examples there. I can't get passed here:

    Code:
    using namespace std;
    
    class X {
    	public:
    		X() {};
    		int foo (int a, int b) { return a+b; }
    };
    
    
    int main(int argc, char *argv[]) {
    	X test();
    	int(X::*func)(int,int) = &X::foo;
    
    	int n = test.*func(1,2);
    
    	return
    I get a rather baffling error for the line in red:

    error: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘func (...)’, e.g. ‘(... ->* func) (...)’

    Implying that line does not use that syntax, in which case I have not idea what that syntax should be since it matches all the example I could find.

    As for a function pointer to a template function, I've never tried, but evidently you are not the first to feel such a need:

    C++ pointer to template function
    Last edited by MK27; 07-02-2011 at 06:43 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    If you want to pass a pointer to a non-static member function of X as an argument of a static member of X, the declaration (inside class X) needs to be of the form
    Code:
       static int bar(int (X::*func)(int, int));
    This example assumes you wish to pass a member function that accepts two int arguments, and returns int. Note that, to implement such a function, it is necessary to have an object for the member function to act on.
    Code:
    int X::bar(int (X::*func)(int, int))
    {
           X some_object;
           X *pointer = &some_object;
           return some_object.*func(10,20) + pointer->*func(30,40);    //   note we are calling the same member function twice, in different ways
    }
    The syntax to call the function is something like
    Code:
    int main()
    {
         return X::bar(&X::foo);
    }
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Code:
    int(*func)(int, int);
    I did not want to create a function pointer.
    I wanted to tell the member function function bar that it can take function pointers to foo (maybe of other objects).
    I could do it with typeof(&X::foo) ...but am looking for a more portable solution....(typeof() of gcc).




    [Noticed the last post after writing this]
    Last edited by manasij7479; 07-02-2011 at 06:44 AM. Reason: Didn't notice Grumpy's post before writing this

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by MK27 View Post
    [I think I may be wrong about that, because foo is actually a method of X, it implicitly takes an object as the first arg, but I do not think you can spoof this in C++...]
    I was wrong and the object is an issue so I replaced most of that post without something more correct.

    Quote Originally Posted by manasij7479 View Post
    I did not want to create a function pointer.
    I wanted to tell the member function function bar that it can take function pointers to foo (maybe of other objects).
    In order to do that you need a function pointer as a parameter to bar. The best source of info I can find is the link in the correction to my last post (not the LMGTFY one ).
    Last edited by MK27; 07-02-2011 at 06:47 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by MK27 View Post
    Code:
    using namespace std;
    
    class X {
    	public:
    		X() {};
    		int foo (int a, int b) { return a+b; }
    };
    
    
    int main(int argc, char *argv[]) {
    	X test();
    	int(X::*func)(int,int) = &X::foo;
    
    	int n = test.*func(1,2);
    
    	return
    I get a rather baffling error for the line in red:

    error: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘func (...)’, e.g. ‘(... ->* func) (...)’
    test is a function prototype, so you get that error, see this.

    I get no compiling errors or warnings with the following:
    Code:
    class X {
    public:
       int foo(int j, int k);
       static int bar(/*ARGS*/);
    };
    
    int qux()
    {
       int (X::*fptr)(int, int) = 0;
       X thing;
       fptr = &X::foo;
    
       int answer = (thing.*fptr) (2, 3);
       return answer;
    }
    Now I think that's the answer, since you can call any suitable member of an instance of X through the pointer fptr.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by MK27 View Post
    I get a rather baffling error for the line in red:

    error: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘func (...)’, e.g. ‘(... ->* func) (...)’
    Okay, I got that to work (need parantheses around identifier):

    Code:
    #include <iostream>
    
    using namespace std;
    
    class X {
    	public:
    		X() {};
    		int foo (int a, int b) { return a+b; }
    };
    
    
    int main(int argc, char *argv[]) {
    	X a;
    	int(X::*func)(int,int) = &X::foo;
    
    	cout << (a.*func)(1,2) << endl;
    
    	return 0;
    }
    But I cannot get it to work the way you want:

    Code:
    #include <iostream>
    
    using namespace std;
    
    class X {
    	public:
    		X() {};
    		int foo (int a, int b) { return a+b; }
    		void test (X *obj, int(X::*func)(int,int)) {
    			cout (obj->*func)(1,2);
    		}
    };
    
    
    int main(int argc, char *argv[]) {
    	X a, b;
    	int(X::*func)(int,int) = &X::foo;
    
    	a.test(&b, func);
    
    	return 0;
    }
    test.cpp: In member function ‘void X::test(X*, int (X::*)(int, int))’:
    test.cpp:10:20: error: invalid use of non-static member function



    [Edit: error is actually caused by missing operator to cout in X::test ]
    Last edited by MK27; 07-02-2011 at 07:33 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by whiteflags View Post
    test is a function prototype, so you get that error, see this.
    Yeah, still wet behind the ears with C++. But the actual error persisted, it was because of the difference between:

    Code:
    a.*func(1,2);  // nope
    (a.*func)(1,2); // okay
    Now I think that's the answer, since you can call any suitable member of an instance of X through the pointer fptr.
    How about getting that to work as a param to a method (2nd example post #7) which is what the OP wants? I'm starting to think it cannot be done (a method cannot accept a function pointer to another method of the same class).
    Last edited by MK27; 07-02-2011 at 07:14 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Shouldn't that be
    Code:
    int(X::*func)(int,int) = &b::foo;
    (Edit: although I'm being lazy and haven't actually tried it.)

    More edit: ha ha what am I thinking? Don't even bother with that.

    On the other hand, that isn't at all what I thought the OP wanted; I thought he wanted something more like
    Code:
    a.test(func);
    Although, the OP said it should only work with foo, which in that case, why are we bothering with pointers to functions?
    Code:
    static int X::bar(X object) {
        object.foo();
    }
    Last edited by tabstop; 07-02-2011 at 07:21 AM.

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Seems like you almost had it MK27. The only thing I did here change the cout statement.

    Code:
    C:\Documents and Settings\Owner\My Documents>g++ -c -Wall -ansi -pedantic test.c
    pp
    
    C:\Documents and Settings\Owner\My Documents>more test.cpp
    #include <iostream>
    using namespace std;
    class X {
            public:
                    X() {};
                    int foo (int a, int b) { return a+b; }
                    void test (X *obj, int(X::*func)(int,int)) {
                            cout << (obj->*func)(1,2);
                    }
    };
    
    
    int main(int argc, char *argv[]) {
            X a, b;
            int (X::*func)(int, int) = &X::foo;
    
            a.test(&b,  func);
    
            return 0;
    }

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by tabstop View Post
    Although, the OP said it should only work with foo, which in that case, why are we bothering with pointers to functions?
    Code:
    static int X::bar(X object) {
        object.foo();
    }
    Well, this is true. That is a whole lot simpler and ensures that only foo will be called of instances of X.

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by whiteflags View Post
    Seems like you almost had it MK27. The only thing I did here change the cout statement.
    Palm->face. Okay, I have the coffee now.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #13
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Although, the OP said it should only work with foo, which in that case, why are we bothering with pointers to functions?Code:

    Code:
    static int X::bar(X object) {    object.foo();}

    Well, this is true. That is a whole lot simpler and ensures that only foo will be called of instances of X.
    Oh...Can't imagine I couldn't think of that...!!
    It'd solve my current problem in a very elegant way...
    I had two objects...and a static function for connecting them..
    Code:
    connect(X a,X b) {b.func2(a.func1());}
    //would be the skeletal structure of it..apart from exception handling/..
    Last edited by manasij7479; 07-02-2011 at 07:43 AM.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by manasij7479 View Post
    I could do it with typeof(&X::foo) ...but am looking for a more portable solution....(typeof() of gcc).
    Alternative to typeof:
    decltype (C++11 only)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Alternative to typeof:
    decltype (C++11 only)
    Wonderful... Combined with auto, I wouldn't need typedefs that much.
    Last edited by manasij7479; 07-03-2011 at 08:38 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-01-2010, 01:05 PM
  2. Function syntax to return a pointer...
    By tzuch in forum C Programming
    Replies: 1
    Last Post: 05-29-2008, 07:53 AM
  3. HELP!! Function Definition problem? Syntax Error I think..
    By felixgun in forum C++ Programming
    Replies: 12
    Last Post: 11-04-2006, 04:49 AM
  4. struct pointer to function syntax
    By kermit in forum C Programming
    Replies: 2
    Last Post: 03-21-2004, 04:01 PM
  5. syntax to pass a member function pointer to another class?
    By reanimated in forum C++ Programming
    Replies: 4
    Last Post: 11-27-2003, 05:24 PM