473,465 Members | 1,399 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

function name hiding

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 -------

Aug 28 '07 #1
3 2804
On Aug 28, 2:41 pm, bb <muralibal...@gmail.comwrote:
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

Aug 28 '07 #2
On Aug 28, 3:20 pm, Neelesh Bodas <neelesh.bo...@gmail.comwrote:
On Aug 28, 2:41 pm, bb <muralibal...@gmail.comwrote:
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
Along with the answer of Neelesh, you will like to see this page by
Bjarne Stroustroup:-
http://www.research.att.com/~bs/bs_f...verloadderived

Regards,
RM

Aug 28 '07 #3
On 2007-08-28 05:41:57 -0400, bb <mu**********@gmail.comsaid:
>
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.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Aug 28 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: atomo | last post by:
Hi, i'm wondering why this hiding and showing function works on IE (6) but it doesn't work in Mozilla Firebird (0.7) function fnc_show(num){ for(i=1;i<7;i++){ eval("t_" + i + ".style.display...
4
by: Ken | last post by:
Hello I am trying to change the color of a font in a text box. I have been reading about and trying various examples found it this group, but still can't get it right. Here is where I am...
4
by: Sharon Tal | last post by:
Hi all. I am trying to figure out the differences between overriding and hiding a method name. The only difference i can see, is that with name hiding i can change the method access level. Are...
10
by: bienwell | last post by:
Hi, I have a question about file included in ASP.NET. I have a file that includes all the Sub functions (e.g FileFunct.vb). One of the functions in this file is : Sub TestFunct(ByVal...
9
by: bob | last post by:
Hi, I know there exists a good reason why the designers of c++ decided that function hiding should exist. But I don't know why. Can anybody provide a good reason/example of a case where function...
3
by: Nicolas Castagne | last post by:
Hi all, I have been wondering for a while why function hiding (in a derived class) exists in C++, e.g. why when writing class Base { void foo( int ) {} }; class Derived: public Base { void...
7
by: Steve Pope | last post by:
Is there a reason I am unable to overload foo() in the following code, which does not compile ("no matching function")? int foo(int a) { return(a); } struct goo { int a;
7
by: asdf | last post by:
They looks so similar. Anybody would like to tell me their differences? Thanks a lot.
4
by: poojak | last post by:
I have write two different js function in a external file. one function write a table (I used innerHTML for this.) and another function is for showing/ hideing some rows depending on radio button...
19
by: subramanian100in | last post by:
Stroustrup, in his book TC++PL 3rd Edition, in page 16, under the section '1.8 Advice' has mentioned the following: Don't use global data(use members) Don't use global functions Don't use...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.