Function pointer

This is a discussion on Function pointer within the C++ Programming forums, part of the General Programming Boards category; Isn't this a little difficult to understand ... You would think a return type is just 'float' ... And can't ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    Angry Function pointer

    Isn't this a little difficult to understand ... You would think a return type is just 'float' ... And can't see two functions in this signature ..

    Code:
       // function takes a char and returns a pointer to a function which is taking two
       // floats and returns a float. <opCode> specifies which function to return
       float (*GetPtr1(const char opCode))(float, float)
       {
          if(opCode == '+')  return &Plus;
          if(opCode == '-')  return &Minus;
       }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    No, because of the (* at the beginning of the function name and the )(float, float) after the function parameter, you have said "This is a pointer to a function returning float".

    The syntax for function pointers is definitely a bit messy - but fortunately, we can do things to simplify it, by using typedef. I personally think this makes your code more readable:

    Code:
    typedef float (*OperatorFunc)(float, float);
    
       // function takes a char and returns a pointer to a function which is taking two
       // floats and returns a float. <opCode> specifies which function to return
       OperatorFunc GetPtr1(const char opCode)
       {
          if(opCode == '+')  return &Plus;
          if(opCode == '-')  return &Minus;
       }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    No, because of the (* at the beginning of the function name and the )(float, float) after the function parameter, you have said "This is a pointer to a function returning float".

    The syntax for function pointers is definitely a bit messy - but fortunately, we can do things to simplify it, by using typedef. I personally think this makes your code more readable:

    Code:
    typedef float (*OperatorFunc)(float, float);
    
       // function takes a char and returns a pointer to a function which is taking two
       // floats and returns a float. <opCode> specifies which function to return
       OperatorFunc GetPtr1(const char opCode)
       {
          if(opCode == '+')  return &Plus;
          if(opCode == '-')  return &Minus;
       }
    --
    Mats
    Hi Mats, welcome back ...

    Well, i must say a lot is hidden without typedef... you have added 'GetPtr1' which was not part of the definition i've provided ... That makes it more readable indeed!!

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
       // function takes a char and returns a pointer to a function which is taking two
       // floats and returns a float. <opCode> specifies which function to return
       float (*GetPtr1(const char opCode))(float, float)
       {
          if(opCode == '+')  return &Plus;
          if(opCode == '-')  return &Minus;
       }
    It was there! Just not as visible.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    I prefer this myself:
    Code:
    typedef float (OperatorFunc)(float, float);
    
       // function takes a char and returns a pointer to a function which is taking two
       // floats and returns a float. <opCode> specifies which function to return
       OperatorFunc* GetPtr1(const char opCode)
       {
          if(opCode == '+')  return &Plus;
          if(opCode == '-')  return &Minus;
       }
    It is, after all, a pointer-
    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.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    Member Function Pointer...

    Say i have a member function pointer

    Code:
    class MyClass {
    private:
           typedef void (MyClass::*MyFunct)(const string&);
           MyFunct f;
    
    public :
          void GeneralFunct(const string& s);
    }
    
    .....
    
    {
    
    MyFunct f = &MyClass::GeneralFunct;
    
    this->*f("Hello There");
    
    /* OR */
    
    MyClass *class1;
    class1->*f("Hello There");
    
    /* OR */
    
    MyClass class2;
    class2.*f("Hello There");
    
    }
    Assuming what i've done is correct, why do i always need a Class Pointer to invoke a member function pointer?

    Why can't i just ...
    Code:
           MyClass myclass = &MyClass::GeneralFunct;
           myclass.f("What's wrong with this?");

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    But you can call a function through a class reference...
    Code:
    class foo
    {
    public:
    	void foo2() {}
    };
    
    int main()
    {
    	typedef void (foo::* foo_member_ptr)();
    	foo_member_ptr my_ptr = &foo::foo2;
    	foo f;
    	(f.*my_ptr)();
    	foo& f2 = f;
    	(f2.*my_ptr)();
    	foo* pf = &f;
    	(pf->*my_ptr)();
    }
    Last edited by Elysia; 01-21-2009 at 02:42 AM.
    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.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    But you can call a function through a class reference...
    Code:
    class foo
    {
    public:
    	void foo2() {}
    };
    
    int main()
    {
    	typedef void (foo::* foo_member_ptr)();
    	foo_member_ptr my_ptr = &foo::foo2;
    	foo f;
    	(f.*my_ptr)();
    	foo& f2 = f;
    	(f2.*my_ptr)();
    	foo* pf = &f;
    	(pf->*my_ptr)();
    }
    It thought a reference was the same as a pointer ... I don't see any difference in what you done as to my examples ...

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    It thought a reference was the same as a pointer ... I don't see any difference in what you done as to my examples ...
    There is no such thing as a "function reference", as functions are not variables, and a reference refers to a variable. You could have a reference to a function pointer, but you still have to make the function "variable" a pointer.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Quote Originally Posted by csonx_p View Post
    It thought a reference was the same as a pointer ... I don't see any difference in what you done as to my examples ...
    Note also that the (myclass.*pointer)() syntax is required. The above sample compiles and demonstrates the 3 different methods of calling a member function that exists.
    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.

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    There is no such thing as a "function reference", as functions are not variables, and a reference refers to a variable. You could have a reference to a function pointer, but you still have to make the function "variable" a pointer.

    --
    Mats
    So my question is left unanswered !!! ... anyways, its not an issue ...

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    So my question is left unanswered !!! ... anyways, its not an issue ...
    Sorry, I don't know which question is unanswered...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    There is no such thing as a "function reference", as functions are not variables, and a reference refers to a variable. You could have a reference to a function pointer, but you still have to make the function "variable" a pointer.

    --
    Mats
    Pardon me, i got my mind mixed up somewhere ... I'm referring to a class pointer while you referring to a pointer variable to a function ...

    I understand the whole referencing now .. thanx Elysia & Mats ...

    @Mats, you should see the progress of that Rental Project! but now you slamming the clrscrn .. anyway ...

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    Delegates

    How useful are delegates over members function pointers?

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think either delegates or inheritance with virtual functions are the "right" solutions for ALMOST all cases where pointers to member functions can be used as a solution.

    It would of course help if you actually explained what you are trying to do.

    And I'm all for a nice screen-layout, including correct use of clrscr() and similar functions - however, if you do that, you will have to use system specific functionality.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  3. Function Pointer help
    By Skydt in forum C Programming
    Replies: 5
    Last Post: 12-02-2005, 08:13 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21