| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"developer.new" |
| Date: |
18 Jan 2008 04:02:57 PM |
| Object: |
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
.
|
|
| User: "=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=" |
|
| Title: Re: Question about Name Hiding concept |
18 Jan 2008 04:50:29 PM |
|
|
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
.
|
|
|
|
| User: "Victor Bazarov" |
|
| Title: Re: Question about Name Hiding concept |
18 Jan 2008 04:19:57 PM |
|
|
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
.
|
|
|
|

|
Related Articles |
|
|