O
Ole Nielsby
I'm puzzed by this:
/***code begin***/
class X {};
class Y : public X {};
class A {
public: virtual void m(X x) {std::wcout << L"A\n";}
};
class B : public A {
public: void m(Y y) {std::wcout << L"B\n";}
};
class C : public B {
public: void m(X x) {std::wcout << L"C\n";}
};
void headache()
{
X x;
B *b = new C();
// b->m(x); //won't compile
b->A::m(x); // outputs A
((A*)b)->m(x); // outputs C
};
/***code end***/
Why does B::m(Y) hide A::m(X)?
Is this a VC quirk or is it a C++ standard thing, and in
that case, what's the rationale? I would expect the public
declaration of A::m to be like part of B insofar as B
doesn't redefine m(X).
When I discovered the b->m(x) wouldn't compile, my
first workaround was b->A::m(x); I assumed this was
the way to tell the compiler not to let the B::m(Y) declaration
distract from the A::m(X); but as the example shows, this
has the side effect of disabling the virtual lookup, which
is not what I intended.
So I finally got to ((A*)b)->m(x); but it aint' handsome...
Is this what I'm supposed to write, or am I not supposed
to overload different signatures of the same method name
in different levels? It does make sense to do this in my
project, I stumbled on the problem by accident, not
because i was trying to push the limits of C++.
/***code begin***/
class X {};
class Y : public X {};
class A {
public: virtual void m(X x) {std::wcout << L"A\n";}
};
class B : public A {
public: void m(Y y) {std::wcout << L"B\n";}
};
class C : public B {
public: void m(X x) {std::wcout << L"C\n";}
};
void headache()
{
X x;
B *b = new C();
// b->m(x); //won't compile
b->A::m(x); // outputs A
((A*)b)->m(x); // outputs C
};
/***code end***/
Why does B::m(Y) hide A::m(X)?
Is this a VC quirk or is it a C++ standard thing, and in
that case, what's the rationale? I would expect the public
declaration of A::m to be like part of B insofar as B
doesn't redefine m(X).
When I discovered the b->m(x) wouldn't compile, my
first workaround was b->A::m(x); I assumed this was
the way to tell the compiler not to let the B::m(Y) declaration
distract from the A::m(X); but as the example shows, this
has the side effect of disabling the virtual lookup, which
is not what I intended.
So I finally got to ((A*)b)->m(x); but it aint' handsome...
Is this what I'm supposed to write, or am I not supposed
to overload different signatures of the same method name
in different levels? It does make sense to do this in my
project, I stumbled on the problem by accident, not
because i was trying to push the limits of C++.