Selection of overloaded function

Discussion in 'C++' started by Fred, Mar 11, 2010.

  1. Fred

    Fred Guest

    What does the standard say about which overloaded function to match in
    this situation:

    (Assume classes A and B are defined, and there is an overloaded
    operator defined
    const A& operator = (const void *);

    class Base {
    public:
    A a;
    B b;
    virtual void fun( A a& );
    virtual void fun( B *b, int i=0 );
    };

    class Sub : public Base {

    void fun( A a);
    void fun( B *b, int i=0);
    };

    Then somewhere I make this call:
    A a = new A();
    B b = new B();
    Sub *s = new Sub();

    s->fun( b );

    Should it i:
    1) invoke Sub's function fun(A)
    2) Base's function fun(B,int) with i defaulted to 0
    3) fail with no match

    gcc gives me option 1

    However, if I remove Sub's 2-argument version,
    I get option (3)

    --
    Fred K
     
    Fred, Mar 11, 2010
    #1
    1. Advertising

  2. Fred wrote:

    > What does the standard say about which overloaded function to match in
    > this situation:
    >
    > (Assume classes A and B are defined, and there is an overloaded
    > operator defined
    > const A& operator = (const void *);
    >
    > class Base {
    > public:
    > A a;
    > B b;
    > virtual void fun( A a& );

    This is a syntax error. What do you mean?
    > virtual void fun( B *b, int i=0 );
    > };
    >
    > class Sub : public Base {
    >
    > void fun( A a);
    > void fun( B *b, int i=0);
    > };
    >
    > Then somewhere I make this call:
    > A a = new A();
    > B b = new B();
    > Sub *s = new Sub();
    >
    > s->fun( b );
    >
    > Should it i:
    > 1) invoke Sub's function fun(A)
    > 2) Base's function fun(B,int) with i defaulted to 0
    > 3) fail with no match
    >
    > gcc gives me option 1
    >
    > However, if I remove Sub's 2-argument version,
    > I get option (3)
    >
    > --
    > Fred K
     
    Michael Tsang, Mar 12, 2010
    #2
    1. Advertising

  3. Fred

    Permostat Guest

    On Mar 11, 10:17 pm, "Daniel T." <> wrote:
    > Fred <> wrote:
    > > What does the standard say about which overloaded function to match in
    > > this situation:

    >
    > [Broken code snipped]
    >
    > > Should it i:
    > > 1) invoke Sub's function fun(A)
    > > 2) Base's function fun(B,int) with i defaulted to 0
    > > 3) fail with no match

    >
    > > gcc gives me option 1

    >
    > > However, if I remove Sub's 2-argument version,
    > > I get option (3)

    >
    > Your code is pretty broken unless I'm missing something basic. However
    > it may help you to understand name hiding.
    >
    > class A { };
    > class B { };
    >
    > struct Base {
    >    void foo(A&);
    >
    > };
    >
    > struct Derived : Base {
    >    void foo(B&);
    >
    > };
    >
    > int main() {
    >    A a;
    >    Derived d;
    >    d.foo(a); // will not compile, see below
    >
    > }
    >
    > "foo(A&)" is hidden and "foo(B&)" doesn't work for an argument of type
    > A. To fix this, you have to explicitly expose the Base function with
    > "using Base::foo;" in the Derived class. Like this:
    >
    > struct Derived : Base {
    >    void foo(B&);
    >    using Base::foo;
    >
    >
    >
    > };


    You're obviously trying to hide rape pornography inside decoy files.
    Since this is an illegal action - I will not provide help.

    sperm-
     
    Permostat, Mar 12, 2010
    #3
  4. Fred

    Fred Guest

    On Mar 11, 8:06 pm, Michael Tsang <> wrote:
    > Fred wrote:
    > > What does the standard say about which overloaded function to match in
    > > this situation:

    >
    > > (Assume classes A and B are defined, and there is an overloaded
    > > operator defined
    > >    const A& operator = (const void *);

    >
    > > class Base {
    > >   public:
    > >   A a;
    > >   B b;
    > >     virtual void fun( A a& );

    >
    > This is a syntax error. What do you mean?
    >


    Oops - I meant A &a

    The real question concerns what is the sequence used to match
    overloaded and polymorphed functions.

    It seems to be:
    1) In Sub class, select the function that matches all non-defaulted
    parameter types exactly.
    2) If no exact match, see if one exists that can be matched by type-
    conversion of non-defaulted parameters.
    3) If still no match, repeat (1) and (2) but adding the defaulted
    parameters.
    4) If still no match (and there were ANY functions by that name
    defined in Sub, fail with a 'no match found' error.
    5) If there were no functions by that name defined in Sub, then and
    only then look for a match in the superclass.
    --
    Fred K
     
    Fred, Mar 12, 2010
    #4
  5. Fred

    James Kanze Guest

    On Mar 12, 3:29 pm, Fred <> wrote:
    > On Mar 11, 8:06 pm, Michael Tsang <> wrote:
    > > Fred wrote:
    > > > What does the standard say about which overloaded function
    > > > to match in this situation:


    > > > (Assume classes A and B are defined, and there is an
    > > > overloaded operator defined
    > > > const A& operator = (const void *);


    > > > class Base {
    > > > public:
    > > > A a;
    > > > B b;
    > > > virtual void fun( A a& );


    > > This is a syntax error. What do you mean?


    > Oops - I meant A &a


    > The real question concerns what is the sequence used to match
    > overloaded and polymorphed functions.


    > It seems to be:
    > 1) In Sub class, select the function that matches all non-defaulted
    > parameter types exactly.
    > 2) If no exact match, see if one exists that can be matched by type-
    > conversion of non-defaulted parameters.
    > 3) If still no match, repeat (1) and (2) but adding the defaulted
    > parameters.
    > 4) If still no match (and there were ANY functions by that name
    > defined in Sub, fail with a 'no match found' error.
    > 5) If there were no functions by that name defined in Sub, then and
    > only then look for a match in the superclass.


    Not at all. The sequence is:

    1. Use standard name lookup to find the matching name(s). If
    any of the matching name is not a function or a function
    template, it is an error.

    2. Use ADL to find additional potential names; only functions
    and function templates are considered.

    3. Do template argument deduction on any function templates in
    the resulting set. If deduction fails, throw the function
    template out; if deduction is ambiguous, error; otherwise,
    add the results of the deduction (which is a function) to
    the overload set.

    4. If any of the resulting functions has default arguments,
    replace it with two or more instances, with and without the
    default arguments.

    5. Do overload resolution.

    --
    James Kanze
     
    James Kanze, Mar 13, 2010
    #5
    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. Simon Niederberger
    Replies:
    2
    Views:
    16,795
    Christian Kaufhold
    Jan 7, 2005
  2. Andrew Crowe
    Replies:
    1
    Views:
    4,549
    Andrew Crowe
    Sep 13, 2004
  3. curiousity

    HOWTO autopost the selection list upon selection

    curiousity, Nov 21, 2003, in forum: ASP .Net Mobile
    Replies:
    0
    Views:
    290
    curiousity
    Nov 21, 2003
  4. Loebb
    Replies:
    0
    Views:
    217
    Loebb
    Feb 23, 2004
  5. Replies:
    1
    Views:
    230
Loading...

Share This Page