C++ theory

K

Kalle Rutanen

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

John Carson

Kalle Rutanen said:
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>
 
A

Alf P. Steinbach

* 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.
 
K

Kalle Rutanen

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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top