C++ Function Pointers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • chetan

    #1

    C++ Function Pointers

    How to initialise a function pointer for any particular function in
    C++ ?

    so that it would clearly specify difference between the functions that
    are called by different objects of that class ...
    i.e
    instead of

    a.function();
    or
    b.function();

    use of this -->
    int (*funcptr)(void ); ???? How ?
  • John Harrison

    #2
    Re: C++ Function Pointers


    "chetan" <rolf21@indiati mes.com> wrote in message
    news:4cb1a40c.0 410310701.3cfe0 6c0@posting.goo gle.com...[color=blue]
    > How to initialise a function pointer for any particular function in
    > C++ ?
    >
    > so that it would clearly specify difference between the functions that
    > are called by different objects of that class ...
    > i.e
    > instead of
    >
    > a.function();
    > or
    > b.function();
    >
    > use of this -->
    > int (*funcptr)(void ); ???? How ?[/color]

    What are you asking? How can a function pointer 'specify the difference
    between the functions that are called by different objects of that class'?
    What does that mean?

    I think you're going to have to ask again and choose your words a little
    more carefully this time. I'm sure you know what you mean but I don't think
    its very clear to anyone else. Sometimes it helps to post some pseudo code
    to more clearly express what you want to do.

    john


    Comment

    • Arijit

      #3
      Re: C++ Function Pointers

      rolf21@indiatim es.com (chetan) wrote in message news:<4cb1a40c. 0410310701.3cfe 06c0@posting.go ogle.com>...[color=blue]
      > How to initialise a function pointer for any particular function in
      > C++ ?
      >
      > so that it would clearly specify difference between the functions that
      > are called by different objects of that class ...
      > i.e
      > instead of
      >
      > a.function();
      > or
      > b.function();
      >
      > use of this -->
      > int (*funcptr)(void ); ???? How ?[/color]

      int (*funcptr)(); Don't use void.

      I don't clearly understand what you are asking for, but my guess is you
      want to call a class member function by a function pointer, like you can
      do for ordinary functions. There is a pointer-to-member-function that you
      can use. Something like: int (C::*mem_func_p tr)() where C is the class.

      -Arijit

      Comment

      • Thomas Matthews

        #4
        Re: C++ Function Pointers

        chetan wrote:
        [color=blue]
        > How to initialise a function pointer for any particular function in
        > C++ ?[/color]
        You assign the name of the function to the pointer:
        function_pointe r = Function_Name;

        Also be aware that you can only assign functions that match the
        signature of the function pointer.

        [color=blue]
        > so that it would clearly specify difference between the functions that
        > are called by different objects of that class ...
        > i.e
        > instead of
        >
        > a.function();
        > or
        > b.function();
        >
        > use of this -->
        > int (*funcptr)(void ); ???? How ?[/color]
        Looks like you want a pointer to a member function.
        Read the FAQ for a good explanation:



        --
        Thomas Matthews

        C++ newsgroup welcome message:

        C++ Faq: http://www.parashift.com/c++-faq-lite
        C Faq: http://www.eskimo.com/~scs/c-faq/top.html
        alt.comp.lang.l earn.c-c++ faq:

        Other sites:
        http://www.josuttis.com -- C++ STL Library book

        Comment

        • Ron Natalie

          #5
          Re: C++ Function Pointers

          Thomas Matthews wrote:
          [color=blue]
          > Also be aware that you can only assign functions that match the
          > signature of the function pointer.
          >[/color]
          Not SIGNATURE but pointers of the same TYPE.

          Sorry to be pendantic, but they're not quite the same thing.
          SIGNATURE applies to the name and args and defines overloading
          instances. The function args and return types are part of the
          function TYPE. You can only assign pointers to functions of
          the same type.

          Comment

          • JKop

            #6
            Re: C++ Function Pointers

            chetan posted:
            [color=blue]
            > How to initialise a function pointer for any particular function in
            > C++ ?
            >
            > so that it would clearly specify difference between the functions that
            > are called by different objects of that class ...
            > i.e
            > instead of
            >
            > a.function();
            > or
            > b.function();
            >
            > use of this -->
            > int (*funcptr)(void ); ???? How ?[/color]


            Here's two functions:

            int Blah(double pig)
            {
            return pig - 5;
            }


            char Bleck(int k)
            {
            return k % 9;
            }

            And here's how you call them using function pointers:

            int main()
            {
            int (*p_Blah)(doubl e); //define the pointer variables
            char (*p_Bleck)(int) ;

            p_Blah = Blah; //Assign an address to them
            p_Bleck = Bleck;

            //NB: When you write a fuction's name without
            //parenthesis, you've got the function's address
            }



            -JKop

            Comment

            • Bob Hairgrove

              #7
              Re: C++ Function Pointers

              On Tue, 02 Nov 2004 08:48:15 GMT, JKop <NULL@NULL.NULL > wrote:
              [color=blue]
              >chetan posted:
              >[color=green]
              >> How to initialise a function pointer for any particular function in
              >> C++ ?
              >>
              >> so that it would clearly specify difference between the functions that
              >> are called by different objects of that class ...
              >> i.e
              >> instead of
              >>
              >> a.function();
              >> or
              >> b.function();
              >>
              >> use of this -->
              >> int (*funcptr)(void ); ???? How ?[/color]
              >
              >
              >Here's two functions:
              >
              >int Blah(double pig)
              >{
              > return pig - 5;
              >}
              >
              >
              >char Bleck(int k)
              >{
              > return k % 9;
              >}
              >
              >And here's how you call them using function pointers:
              >
              >int main()
              >{
              > int (*p_Blah)(doubl e); //define the pointer variables
              > char (*p_Bleck)(int) ;
              >
              > p_Blah = Blah; //Assign an address to them
              > p_Bleck = Bleck;
              >
              > //NB: When you write a fuction's name without
              > //parenthesis, you've got the function's address
              >}[/color]

              You haven't called anything. Besides, the OP really wanted to know
              about pointers to member functions, not regular functions.

              --
              Bob Hairgrove
              NoSpamPlease@Ho me.com

              Comment

              • David Lindauer

                #8
                Re: C++ Function Pointers

                There are two types of function pointers.

                1) pointer to a non-member function

                int nn(int b) ; // define a function
                int (*aa)(int b) ; // define a pointer
                aa = nn ; // assign the function to the pointer
                int bb = (*aa)(5) ; // call the function

                2) pointer to a member function

                struct ss {
                int nn(int b) ; // define member function
                } ;
                int (ss::*aa)(int b) ; // define pointer to it, this is NOT a class
                member variable the way it is written
                aa = ss::nn ; // assign function to the pointer
                struct ss *t = new ss ; // make a structure variable
                int c = t->*aa(5) ; // call the member function , can also use ".*" for
                non-pointer instances

                David
                chetan wrote:
                [color=blue]
                > How to initialise a function pointer for any particular function in
                > C++ ?
                >
                > so that it would clearly specify difference between the functions that
                > are called by different objects of that class ...
                > i.e
                > instead of
                >
                > a.function();
                > or
                > b.function();
                >
                > use of this -->
                > int (*funcptr)(void ); ???? How ?[/color]

                Comment

                • JKop

                  #9
                  Re: C++ Function Pointers

                  David Lindauer posted:
                  [color=blue]
                  > There are two types of function pointers.
                  >
                  > 1) pointer to a non-member function
                  >
                  > int nn(int b) ; // define a function[/color]

                  That's "declare a function".
                  [color=blue]
                  > int (*aa)(int b) ; // define a pointer
                  > aa = nn ; // assign the function to the pointer
                  > int bb = (*aa)(5) ; // call the function[/color]

                  Yes; but 99% of the time, that's written as:

                  int bb = aa(5);

                  Comment

                  • David Lindauer

                    #10
                    Re: C++ Function Pointers



                    JKop wrote:
                    [color=blue]
                    > David Lindauer posted:
                    >[color=green]
                    > > There are two types of function pointers.
                    > >
                    > > 1) pointer to a non-member function
                    > >
                    > > int nn(int b) ; // define a function[/color]
                    >
                    > That's "declare a function".
                    >[color=green]
                    > > int (*aa)(int b) ; // define a pointer
                    > > aa = nn ; // assign the function to the pointer
                    > > int bb = (*aa)(5) ; // call the function[/color]
                    >
                    > Yes; but 99% of the time, that's written as:
                    >
                    > int bb = aa(5);[/color]

                    maybe to you... I prefer the former because it makes it very plain you
                    are calling through a variable. Such things make a huge difference when
                    you hand off a program to someone else... they can immediately see what
                    is going on.

                    David

                    Comment

                    • JKop

                      #11
                      Re: C++ Function Pointers

                      David Lindauer posted:
                      [color=blue]
                      >
                      >
                      > JKop wrote:
                      >[color=green]
                      >> David Lindauer posted:
                      >>[color=darkred]
                      >> > There are two types of function pointers.
                      >> >
                      >> > 1) pointer to a non-member function
                      >> >
                      >> > int nn(int b) ; // define a function[/color]
                      >>
                      >> That's "declare a function".
                      >>[color=darkred]
                      >> > int (*aa)(int b) ; // define a pointer
                      >> > aa = nn ; // assign the function to the pointer int
                      >> > bb = (*aa)(5) ; // call the function[/color]
                      >>
                      >> Yes; but 99% of the time, that's written as:
                      >>
                      >> int bb = aa(5);[/color]
                      >
                      > maybe to you... I prefer the former because it makes it very plain you
                      > are calling through a variable. Such things make a huge difference when
                      > you hand off a program to someone else... they can immediately see what
                      > is going on.
                      >
                      > David[/color]


                      But at the same time, the asterisk is redundant.

                      Take a variable/object for example. If it's type is "int*", then when you
                      stick an asterisk in front of it, you've got an expression of type "int".
                      Like so:

                      int* p = ...;

                      *p;


                      Now take a pointer to a function. If it's type is "int (*)(double)", then
                      when you stick an asterisk in front of it, you've got an expression of type
                      "int (*)(double)". Nothing has changed - the asterisk is redundant.


                      Anyway; as regards making it clear that it's being called "through a
                      pointer", I don't see the relevance - it doesn't have to be worked with any
                      differently. Take pointer variables for example, I usually prefix "p_" to
                      their name to clearly indicate that it's a pointer, because pointers are
                      work with differently to normal variables/objects. But this isn't so for
                      pointers to functions, they work exactly the same - there's no need to say
                      "Look at me, I'm really a pointer, keep it in mind".


                      Also, I'd prefer a reference to a function wherever possible.


                      -JKop

                      Comment

                      • David Lindauer

                        #12
                        Re: C++ Function Pointers



                        JKop wrote:
                        [color=blue]
                        > David Lindauer posted:
                        >[color=green]
                        > >
                        > >
                        > > JKop wrote:
                        > >[color=darkred]
                        > >> David Lindauer posted:
                        > >>
                        > >> > There are two types of function pointers.
                        > >> >
                        > >> > 1) pointer to a non-member function
                        > >> >
                        > >> > int nn(int b) ; // define a function
                        > >>
                        > >> That's "declare a function".
                        > >>
                        > >> > int (*aa)(int b) ; // define a pointer
                        > >> > aa = nn ; // assign the function to the pointer int
                        > >> > bb = (*aa)(5) ; // call the function
                        > >>
                        > >> Yes; but 99% of the time, that's written as:
                        > >>
                        > >> int bb = aa(5);[/color]
                        > >
                        > > maybe to you... I prefer the former because it makes it very plain you
                        > > are calling through a variable. Such things make a huge difference when
                        > > you hand off a program to someone else... they can immediately see what
                        > > is going on.
                        > >
                        > > David[/color]
                        >
                        > But at the same time, the asterisk is redundant.
                        >
                        > Take a variable/object for example. If it's type is "int*", then when you
                        > stick an asterisk in front of it, you've got an expression of type "int".
                        > Like so:
                        >
                        > int* p = ...;
                        >
                        > *p;
                        >
                        > Now take a pointer to a function. If it's type is "int (*)(double)", then
                        > when you stick an asterisk in front of it, you've got an expression of type
                        > "int (*)(double)". Nothing has changed - the asterisk is redundant.
                        >
                        > Anyway; as regards making it clear that it's being called "through a
                        > pointer", I don't see the relevance - it doesn't have to be worked with any
                        > differently. Take pointer variables for example, I usually prefix "p_" to
                        > their name to clearly indicate that it's a pointer, because pointers are
                        > work with differently to normal variables/objects. But this isn't so for
                        > pointers to functions, they work exactly the same - there's no need to say
                        > "Look at me, I'm really a pointer, keep it in mind".
                        >
                        > Also, I'd prefer a reference to a function wherever possible.
                        >
                        > -JKop[/color]

                        redundancy is good. It makes it easier to pick up on nuances of code that
                        might otherwise take some studying. I know as a coder it is fun to pick the
                        optimal way of expressing yourself, but the code has a *much* longer life than
                        just the few months you spend coding it, and you may not be there to deal with
                        it throughout that life.

                        David

                        Comment

                        Working...