inheritance and name resolution

S

Srini

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
 
A

Alf P. Steinbach

* Srini:
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)

§7.3.3/12 explicitly allows this, stating that B::foo hides A::foo (the
example given is essentially the same as yours).
 
B

benben

Srini said:
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
 
S

Srini

Got it! A function in the derived class *always* hides the function
with the same signature, in the base class. Thank you Alf and Ben.
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top