function name hiding

B

bb

Hi,

Why overriding "virtual void f(std::string)" as follows hides the "non-
virtual void f(int)" ?

compilation error C2664: 'D::f' : cannot convert parameter 1 from
'int' to 'std::string'

I know it will work if I bring it to the scope by B::f(). I just would
like to know why does it hide just because am implementing a virtual
method.

Thanks in advance.

------ code starts -------

class B {
public:
void f(int i) {
std::cout << "B::f(int)" << std::endl;
}
virtual void f(std::string s) {
std::cout << "virtual B::f(std::string)" << std::endl;
}
};

class D : public B {
public:
void f(std::string s) {
std::cout << "D::f(std::string)" << std::endl;
}
void g() {
std::cout << "D::g()" << std::endl;
f(10); // works if I make it B::f(10)
}
};

------ code ends -------
 
N

Neelesh Bodas

Hi,

Why overriding "virtual void f(std::string)" as follows hides the "non-
virtual void f(int)" ?

class B {
public:
void f(int i) {
std::cout << "B::f(int)" << std::endl;
}
virtual void f(std::string s) {
std::cout << "virtual B::f(std::string)" << std::endl;
}

};

class D : public B {
public:
void f(std::string s) {
std::cout << "D::f(std::string)" << std::endl;
}
void g() {
std::cout << "D::g()" << std::endl;
f(10); // works if I make it B::f(10)
}

};

Because C++ standard says so.
10.2(2): A member name f in one subobject B hides a member name f in a
subobject A if A is a base class subobject of B. Any declarations that
are so hidden are eliminated from consideration.

Thus D::f(std::string) will hide B::f(int i) as well as
B::f(std::string s)

-N
 
R

Reetesh Mukul

Because C++ standard says so.
10.2(2): A member name f in one subobject B hides a member name f in a
subobject A if A is a base class subobject of B. Any declarations that
are so hidden are eliminated from consideration.

Thus D::f(std::string) will hide B::f(int i) as well as
B::f(std::string s)

-N

Along with the answer of Neelesh, you will like to see this page by
Bjarne Stroustroup:-
http://www.research.att.com/~bs/bs_faq2.html#overloadderived

Regards,
RM
 
P

Pete Becker

Why overriding "virtual void f(std::string)" as follows hides the "non-
virtual void f(int)" ?

Overloading applies to names defined in the same scope. B::f and D::f
are defined in different scopes, so overloading is not applicable. That
provides a bit of protection from surprises during maintenance:

class B
{
};

class D : public B
{
public:
void f(double);
};

D d;
d.f(3);

Now suppose that during maintenance (perhaps installing a new version
of a library that provides the base class B) a new member is added to B:

class B
{
public:
void f(int);
};

If overloading wasn't limited to scopes, the behavior of d.f(3) would
change: it would call B::f instead of D::f. That would almost certainly
be wrong: the designer of B typically has no knowledge of what D is
doing, and B::f, while appropriate for use in B, wouldn't do anything
sensible for a D object.
 

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

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,824
Latest member
Nater888

Latest Threads

Top