"Srini" <> wrote in message
news: oups.com...
> Hello,
>
> I've had this long lasting doubt. When a sub-class re-defines a
> function that's in its superclass, it hides the superclass function.
>
> class A {
> public:
> void foo(int) { }
> };
>
> class B : public A {
> public:
> void foo(std::string) { }
> };
>
> B obj;
> obj.foo(10); // Error, no matching function B::foo(int)
>
> Here B::foo(std::string) hides A::foo(int). So, if I want to bring
> A::foo(int) into visibility, I've to put a using directive.
>
> class B : public A {
> public:
> using A::foo; // bring A::foo(int) into scope here
> void foo(std::string) { }
> };
>
> Now the normal overload function call resolution would happen. In that
> case, why is the following not an error?
>
> class A {
> public:
> void foo(int) { }
> };
>
> class B : public A {
> public:
> using A::foo;
> void foo(int) { }
> };
>
> B obj;
> obj.foo(10); // This would call B::foo(int)
>
> Should this not result in a compile error because there are 2 functions
> with the same signature?
>
> Thanks,
> Srini
>
Consider:
class base
{
public:
void foo(int);
void foo(double);
void foo(std::string);
};
You are now require to override void base::foo(int), and also make the other
two overloads visible, how would you do?
class derived: public base
{
public:
using base::foo;
void foo(int);
};
Ben
|