473,394 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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_int" << endl; return 1; }
int f (string s_) { cout << "Base::f_str" << 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 4806
developer.new wrote:
I have a question regarding this concept I learned about recently:
Name Hiding. [..]

class Base {
public:
int f () { cout << "Base::f_int" << endl; return 1; }
int f (string s_) { cout << "Base::f_str" << 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_int" << endl; return 1; }
int f (string s_) { cout << "Base::f_str" << 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
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...
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: 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...
1
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
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...
3
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' ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.