Re: Derived class hiding a name
jordigh@gmail.com wrote [2008-05-22 6:57 AM]:
The following code will not compile:
class foo {};
class A {
public: void f(int a ){a++;};
private: virtual void f(foo a) = 0;
};
class B : public A {
private: virtual void f(foo a){};
};
int main() { B b; int a=0; b.f(a); }
The problem seems to be that all of my functions being named f are
somehow colliding with each other. It seems to me that the
call b.f(a) is unambiguosly pointing to A::f(int), but gcc disagrees.
[...]
If a using declaration should be enough to fix this, where should I
place? Or a better fix?
Because you declared "b" as of type "B", for function resolution,
only the members of B are candidates, so A::f(int) is not considered
unless it's declared to be part of B by making it virtual or adding
a "using" statement. That leaves the only candidate in B as
B::f(foo), which doesn't match the signature and can't be called
because it's private.
If you just add "using A::f" to B, however, there are overloaded
private functions named A::f, so that still leaves an error.
The overloaded functions in A must be protected instead of private.
class A {
public: void f(int a ) { a++; }
protected: virtual void f(foo a) = 0;
};
class B : public A {
public: using A::f;
private: virtual void f(foo a) {}
};
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]