Connecting Tech Pros Worldwide Help | Site Map

C++ theory

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 01:59 AM
Kalle Rutanen
Guest
 
Posts: n/a
Default C++ theory

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;
}

  #2  
Old July 23rd, 2005, 01:59 AM
John Carson
Guest
 
Posts: n/a
Default 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

  #3  
Old July 23rd, 2005, 01:59 AM
Alf P. Steinbach
Guest
 
Posts: n/a
Default 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?
  #4  
Old July 23rd, 2005, 01:59 AM
Kalle Rutanen
Guest
 
Posts: n/a
Default 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!
  #5  
Old July 23rd, 2005, 01:59 AM
Kalle Rutanen
Guest
 
Posts: n/a
Default 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!
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.