Connecting Tech Pros Worldwide Forums | Help | Site Map

C++ theory

Kalle Rutanen
Guest
 
Posts: n/a
#1: Jul 23 '05
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;
}

John Carson
Guest
 
Posts: n/a
#2: Jul 23 '05

re: C++ theory


"Kalle Rutanen" <none@here.com> wrote in message
news:d118mn$jq5$1@news.cc.tut.fi[color=blue]
> 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;
> }[/color]


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

Alf P. Steinbach
Guest
 
Posts: n/a
#3: Jul 23 '05

re: C++ theory


* Kalle Rutanen:[color=blue]
> Why does this short program compile (compiled fine on msvcnet2003) ?[/color]

Because that the standard says it should.

[color=blue]
> 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;
> }[/color]

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?
Kalle Rutanen
Guest
 
Posts: n/a
#4: Jul 23 '05

re: C++ theory


> In some contexts, you would be right, but not in this context. Section[color=blue]
> 7.3.3, p12 of the C++ standard says:[/color]

Great thanks!
Kalle Rutanen
Guest
 
Posts: n/a
#5: Jul 23 '05

re: C++ theory


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

Thank you!
Closed Thread