473,548 Members | 2,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about Name Hiding concept

Hi

I have a question regarding this concept I learned about recently:
Name Hiding. Here's what I've come across:
There is a base class with two functions with the same name but
different signature. A class inherits publicly from this base class
and redefines one of the two functions in the derived class. In that
case, a derived class object cannot access the other base class
function that it hasn't redefined. I'm posting a code snippet to make
it clear.

class Base {
public:
int f () { cout << "Base::f_in t" << endl; return 1; }
int f (string s_) { cout << "Base::f_st r" << endl; return 1; }
};

class Derived : public Base {
public:
int f() { cout << "Derived::f_int " << endl; return 1;}
};

int main() {
string str_ = "blahhh";
Derived d1;
d1.f();
d1.f(str_); //Gives compilation error!!!!!!
d1.Base::f(str_ ); //This of course, works.

return 0;
}

Going by how C++ uses name mangling to create unique function names, I
thought it would be possible to access the Base class "int f (string)"
function directly from the derived object.

Any idea why this cannot be done? I know it's a not a good practice to
have functions with duplicate names and all that. But my question is
more about inheritance. Why does this function become inaccessible
from the derived class?

Would really appreciate answers :)

- ND
Jan 18 '08 #1
2 4836
developer.new wrote:
I have a question regarding this concept I learned about recently:
Name Hiding. [..]

class Base {
public:
int f () { cout << "Base::f_in t" << endl; return 1; }
int f (string s_) { cout << "Base::f_st r" << endl; return 1; }
};

class Derived : public Base {
public:
int f() { cout << "Derived::f_int " << endl; return 1;}
};

int main() {
string str_ = "blahhh";
Derived d1;
d1.f();
d1.f(str_); //Gives compilation error!!!!!!
d1.Base::f(str_ ); //This of course, works.

return 0;
}

Going by how C++ uses name mangling to create unique function names, I
thought it would be possible to access the Base class "int f (string)"
function directly from the derived object.
"Possible" does not necessarily mean "good". Name hiding does not
just come into play with classes. Any time you declare an object
in some scope with the same name as another object declared in the
outer scope, the outer object's name is _hidden_ by the name of the
newly declared object.
Any idea why this cannot be done? I know it's a not a good practice to
have functions with duplicate names and all that. But my question is
more about inheritance. Why does this function become inaccessible
from the derived class?
It does not become inaccessible. You show in the code above that
it _is_ accessible. You just have to use a differen syntax to
let the compiler find it.

Have you tried looking in the archives for any explanation about
name hiding? This "issue" comes up periodically, and repeating
what's been told countless times seems really rather unnecessary.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 18 '08 #2
On 2008-01-18 23:02, developer.new wrote:
Hi

I have a question regarding this concept I learned about recently:
Name Hiding. Here's what I've come across:
There is a base class with two functions with the same name but
different signature. A class inherits publicly from this base class
and redefines one of the two functions in the derived class. In that
case, a derived class object cannot access the other base class
function that it hasn't redefined. I'm posting a code snippet to make
it clear.

class Base {
public:
int f () { cout << "Base::f_in t" << endl; return 1; }
int f (string s_) { cout << "Base::f_st r" << endl; return 1; }
};

class Derived : public Base {
public:
int f() { cout << "Derived::f_int " << endl; return 1;}
};

int main() {
string str_ = "blahhh";
Derived d1;
d1.f();
d1.f(str_); //Gives compilation error!!!!!!
d1.Base::f(str_ ); //This of course, works.

return 0;
}

Going by how C++ uses name mangling to create unique function names, I
thought it would be possible to access the Base class "int f (string)"
function directly from the derived object.

Any idea why this cannot be done? I know it's a not a good practice to
have functions with duplicate names and all that. But my question is
more about inheritance. Why does this function become inaccessible
from the derived class?
It is because of the way names are looked up. IIRC it goes something
like this, first the current context is searched for any function with
the right name, if you find one or more goto next step, if not search in
a "outer" context (such as base-classes, global namespaces etc.). Next
the list of functions found are examined to see if any of them matches
the function arguments (and if none is found you try with conversions).

Since a name is found in the derived class the base-class is never
searched and thus the matching function will not be found. Why things
are done is this way you will have to ask in comp.std.c++.

--
Erik Wikström
Jan 18 '08 #3

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

Similar topics

9
3346
by: middletree | last post by:
I'm doing an ASP-built Intranet app, and am having trouble with a bit of code on the menu I am using. I got it from some free site, but I tried emailing the person whose email address is in the code, but that email addy doesn't work anymore. Since it's Intranet, I put an HTML version on the web for you to look at. The links won't work,...
4
4143
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 there any other differences? Thanks, Sharon.
10
1319
by: Rob Meade | last post by:
Hi all, I've got myself into a bit of a problem and I'm trying to dig my way back out, in a) the quickest way due to project deadline and also b) best approach. I have a form which gets a set of document type properties from SQL Server, these properties are used to create corresponding controls on the form, so for example:
1
1037
by: Shapper | last post by:
Hello, I created a script to send an email from a contact form. Is it possible to send the name of the sender? I know I have the fields: mdg.From msg.To msg.Subject
8
3515
by: John K | last post by:
It seems to work, at least my application still works. After adding this tag to hide the documentation; is it normal to see an error message when browsing to the page? I get error message shown below when going to that page in Internet Explorer on the same machine as the service. If I understand correctly; to correct this error I simply...
3
2811
by: bb | last post by:
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
0
7518
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7444
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7711
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7467
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7805
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6039
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5367
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5085
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3497
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...

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.