C++ theory

Discussion in 'C++' started by Kalle Rutanen, Mar 13, 2005.

  1. Why does this short program compile (compiled fine on msvcnet2003) ?

    class A
    {
    public:
    void set()
    {
    }
    };

    class B: public A
    {
    public:
    using A::set;

    void set()
    {
    }
    };

    int main()
    {
    B b;

    // Shouldn't this call be ambiguous ?
    b.set();

    return 0;
    }
    Kalle Rutanen, Mar 13, 2005
    #1
    1. Advertising

  2. Kalle Rutanen

    John Carson Guest

    "Kalle Rutanen" <> wrote in message
    news:d118mn$jq5$
    > Why does this short program compile (compiled fine on msvcnet2003) ?
    >
    > class A
    > {
    > public:
    > void set()
    > {
    > }
    > };
    >
    > class B: public A
    > {
    > public:
    > using A::set;
    >
    > void set()
    > {
    > }
    > };
    >
    > int main()
    > {
    > B b;
    >
    > // Shouldn't this call be ambiguous ?
    > b.set();
    >
    > return 0;
    > }



    In some contexts, you would be right, but not in this context. Section
    7.3.3, p12 of the C++ standard says:

    <quote>
    When a using-declaration brings names from a base class into a derived class
    scope, member functions in the derived class override and/or hide member
    functions with the same name and parameter types in a base class (rather
    than conflicting). [Example:

    struct B {
    virtual void f(int);
    virtual void f(char);
    void g(int);
    void h(int);
    };

    struct D : B {
    using B::f;
    void f(int); // OK: D::f(int) overrides B::f(int);
    using B::g;
    void g(char); // OK
    using B::h;
    void h(int); // OK: D::h(int) hides B::h(int)
    };

    void k(D* p)
    {
    p->f(1); //calls D::f(int)
    p->f('a'); //calls B::f(char)
    p->g(1); //calls B::g(int)
    p->g('a'); //calls D::g(char)
    }
    -end example] [Note: two using-declarations may introduce functions with the
    same name and the same parameter types. If, for a call to an unqualified
    function name, function overload resolution selects the functions introduced
    by such using-declarations, the function call is ill-formed. ]

    </quote>


    --
    John Carson
    John Carson, Mar 13, 2005
    #2
    1. Advertising

  3. * Kalle Rutanen:
    > Why does this short program compile (compiled fine on msvcnet2003) ?


    Because that the standard says it should.


    > class A
    > {
    > public:
    > void set()
    > {
    > }
    > };
    >
    > class B: public A
    > {
    > public:
    > using A::set;
    >
    > void set()
    > {
    > }
    > };
    >
    > int main()
    > {
    > B b;
    >
    > // Shouldn't this call be ambiguous ?
    > b.set();
    >
    > return 0;
    > }


    No. §7.3.3/12 states that there is no conflict. The 'set' declaration in B
    has the same name and signature as the declaration brought in by the 'using',
    and therefore hides that.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Mar 13, 2005
    #3
  4. > In some contexts, you would be right, but not in this context. Section
    > 7.3.3, p12 of the C++ standard says:


    Great thanks!
    Kalle Rutanen, Mar 13, 2005
    #4
  5. > No. §7.3.3/12 states that there is no conflict. The 'set' declaration in B
    > has the same name and signature as the declaration brought in by the 'using',
    > and therefore hides that.


    Thank you!
    Kalle Rutanen, Mar 13, 2005
    #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. Ron Weldy
    Replies:
    2
    Views:
    374
    Kristofer Gafvert
    Jan 29, 2005
  2. =?Utf-8?B?ZGhucml2ZXJzaWRl?=

    wma / drm / music download theory help!

    =?Utf-8?B?ZGhucml2ZXJzaWRl?=, Mar 6, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    358
    Scott Allen
    Mar 6, 2005
  3. Amelyan

    My ASP.NET Page theory!

    Amelyan, May 18, 2005, in forum: ASP .Net
    Replies:
    8
    Views:
    439
    Lucas Tam
    May 20, 2005
  4. Barak
    Replies:
    0
    Views:
    806
    Barak
    Aug 7, 2003
  5. String Theory

    , Apr 28, 2005, in forum: Java
    Replies:
    17
    Views:
    657
    Thomas G. Marshall
    May 23, 2005
Loading...

Share This Page