Pointers to functions: How is the proper function selected?

Discussion in 'C++' started by Aguilar, James, Jul 12, 2004.

  1. I want to know how C++ selects the correct function when there are two
    functions of the same name which might be selected.

    Here is an example.

    class Foo
    {
    public:
    void fooBar(); //(m1)
    void fooBar(int); //(m2)
    }

    int main()
    {
    void (*ptrToMeth)(int) = Foo::fooBar; //(1)
    void (*ptrToMeth2)() = Foo::fooBar; //(2)

    return 0;
    }

    I know that the pointer to fooBar is resolved based on its name. However,
    how do the assignments (1) and (2) know that they are supposed to point to
    (m2) and (m1) respectively?

    Thanks for the help in advance.
    Aguilar, James, Jul 12, 2004
    #1
    1. Advertising

  2. Aguilar, James

    Phlip Guest

    Aguilar, James wrote:

    > int main()
    > {
    > void (*ptrToMeth)(int) = Foo::fooBar; //(1)
    > void (*ptrToMeth2)() = Foo::fooBar; //(2)


    > I know that the pointer to fooBar is resolved based on its name. However,
    > how do the assignments (1) and (2) know that they are supposed to point to
    > (m2) and (m1) respectively?


    I adjust your code to something I suspect has better odds of compiling. No
    class in the way to bring "pointers to member functions" into the question:

    void fooBar(); //(m1)
    void fooBar(int); //(m2)

    void (*ptrToMeth)(int) = fooBar; //(1)
    void (*ptrToMeth2)() = fooBar; //(2)

    The answer is this: That little 'int' reaches an invisible arm out to the
    right, passed the = sign, grabs that 'fooBar', and twists it. The 'fooBar'
    is really a little list of 'fooBar' overloads, and the 'int' picks the
    instance it matches.

    From here, grab the C++ Faq (Google for it) and look up "member function
    pointers". After you learn them, you can ask the same question, and they'l
    have generally the same answer.

    --
    Phlip
    http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces
    Phlip, Jul 12, 2004
    #2
    1. Advertising

  3. "Phlip" <> wrote in message
    news:w7oIc.161$...
    > Aguilar, James wrote:
    >
    > [snip]


    Sweet action. I had thought it might be something like this, but I could
    not find anything about it on the website. I'm going to look up the member
    functions on the FAQ now! Thanks much!
    Aguilar, James, Jul 12, 2004
    #3
  4. On Sun, 11 Jul 2004 23:48:24 -0400, Aguilar, James
    <> wrote:

    > I want to know how C++ selects the correct function when there are two
    > functions of the same name which might be selected.
    >


    By examining the type of the variable being assigned to is the short
    answer.

    > Here is an example.
    >
    > class Foo
    > {
    > public:
    > void fooBar(); //(m1)
    > void fooBar(int); //(m2)
    > }
    >
    > int main()
    > {
    > void (*ptrToMeth)(int) = Foo::fooBar; //(1)
    > void (*ptrToMeth2)() = Foo::fooBar; //(2)
    >
    > return 0;
    > }
    >
    > I know that the pointer to fooBar is resolved based on its name.
    > However,
    > how do the assignments (1) and (2) know that they are supposed to point
    > to
    > (m2) and (m1) respectively?
    >
    > Thanks for the help in advance.
    >


    Your code is illegal. Perhaps you mean this?

    class Foo
    {
    public:
    void fooBar(); //(m1)
    void fooBar(int); //(m2)
    };

    int main()
    {
    void (Foo::*ptrToMeth)(int) = &Foo::fooBar; //(1)
    void (Foo::*ptrToMeth2)() = &Foo::fooBar; //(2)
    return 0;
    }

    C++ allows this in several contexts (this is from 13.4 para 1 if
    C++ standard)

    a) object or reference being initalised
    b) left hand side of assignment
    c) function parameter
    d) user defined operator parameter
    e) return value
    f) explicit type conversion
    g) non-type template parameter

    Hard to imagine any other situations in which you would want to do this.

    AFAIK exactly one of the overloaded functions must match exactly the type
    of the target.

    john
    John Harrison, Jul 12, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. S?ren Gammelmark
    Replies:
    1
    Views:
    1,879
    Eric Sosman
    Jan 7, 2005
  2. Marc Thrun
    Replies:
    15
    Views:
    857
    Tim Rentsch
    Oct 4, 2005
  3. newbie

    Pointers to char pointers in functions

    newbie, Sep 18, 2006, in forum: C Programming
    Replies:
    9
    Views:
    306
    August Karlstrom
    Sep 24, 2006
  4. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    668
  5. ajaybgr
    Replies:
    18
    Views:
    963
    Philip Lantz
    Sep 7, 2012
Loading...

Share This Page