Paragraph 10.3/2 in the C++ standard [ISO/IEC 14882:1998] has the
following code example:
<quote>
struct A {
virtual void f();
};
struct B : virtual A {
virtual void f();
};
struct C : B , virtual A {
using A::f;
};
void foo() {
C c;
c.f(); // calls B::f, the final overrider
c.C::f(); // calls A::f because of the usingdeclaration
}
</quote>
It seems a little odd (to me) that the method call 'c.f()' in function
foo() should invoke B::f. I thought it would invoke A::f due to the
using declaration 'using A::f;' in class C's declaration block. And in
fact, when I use G++ 3.3 to build the code sample shown below, the
method call 'c.f()' invokes A::f (which is what I expected), and not
B::f as stated in the C++ standard.
<example>
<code main.cpp>
#include <iostream>
struct A {
virtual void f() { std::cout << "A::f()\n"; }
};
struct B : virtual A {
virtual void f() { std::cout << "B::f()\n"; }
};
struct C : B, virtual A {
using A::f;
};
int main()
{
C c;
c.f(); // g++ 3.3 invokes A::f, not B::f
c.C::f();
}
</code main.cpp>
<build>
g++ main.cpp
</build>
<output>
A::f()
A::f()
</output>
</example>
FWIW, I looked around on the "JTC1/SC22/WG21 - C++" web site,
http://anubis.dkuug.dk/jtc1/sc22/wg21/
but I didn't find anything about this particular issue (e.g., in the
"Core Language Issues" pages). So I'd like some verification that c.f()
is indeed supposed to invoke B::f, as stated in the standard, and not A::f.
--
Jim
To reply by email, remove "link" and change "now.here" to "yahoo"
jfischer_link5809{at}now.here.com