Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8375811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:16:21+00:00 2026-06-09T15:16:21+00:00

Possible Duplicate: name hiding and fragile base problem I’m familiar with the rules involving

  • 0

Possible Duplicate:
name hiding and fragile base problem

I’m familiar with the rules involving member function hiding. Basically, a derived class with a function that has the same name as a base class function doesn’t actually overload the base class function – it completely hides it.

struct Base
{
    void foo(int x) const
    {

    }
};

struct Derived : public Base
{
    void foo(const std::string& s) { }
};


int main()
{
    Derived d;
    d.foo("abc");
    d.foo(123); // Will not compile! Base::foo is hidden!
}

So, you can get around this with a using declaration. But my question is, what is the reason for base class function hiding? Is this a “feature” or just a “mistake” by the standards committee? Is there some technical reason why the compiler can’t look in the Base class for matching overloads when it doesn’t find a match for d.foo(123)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:16:22+00:00Added an answer on June 9, 2026 at 3:16 pm

    Name lookup works by looking in the current scope for matching names, if nothing is found then it looks in the enclosing scope, if nothing is found it looks in the enclosing scope, etc. until reaching the global namespace.

    This isn’t specific to classes, you get exactly the same name hiding here:

    #include <iostream>
    
    namespace outer
    {
      void foo(char c) { std::cout << "outer\n"; }
    
      namespace inner
      {
        void foo(int i) { std::cout << "inner\n"; }
    
        void bar() { foo('c'); }
      }
    }
    
    int main()
    {
      outer::inner::bar();
    }
    

    Although outer::foo(char) is a better match for the call foo('c') name lookup stops after finding outer::inner::foo(int) (i.e. outer::foo(char) is hidden) and so the program prints inner.

    If member function name weren’t hidden that would mean name lookup in class scope behaved differently to non-class scope, which would be inconsistent and confusing, and make C++ even harder to learn.

    So there’s no technical reason the name lookup rules couldn’t be changed, but they’d have to be changed for member functions and other types of name lookup, it would make compilers slower because they’d have to continue searching for names even after finding matching names in the current scope. Sensibly, if there’s a name in the current scope it’s probably the one you wanted. A call in a scope A probably wants to find names in that scope, e.g. if two functions are in the same namespace they’re probably related (part of the same module or library) and so if one uses the name of the other it probably means to call the one in the same scope. If that’s not what you want then use explicit qualification or a using declaration to tell the compiler the other name should be visible in that scope.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Printing derived class name in base class I am using GCC, and
Possible Duplicate: Is it ever possible to get the current (member) function name in
Possible Duplicate: Determine original name of variable after its passed to a function. I
Possible Duplicate: Finding the Variable Name passed to a Function in C# In C#,
Possible Duplicate: Is array name a pointer in C? Array Type - Rules for
Possible Duplicate: How can I get the name of function inside a JavaScript function?
Possible Duplicate: Access return value from Thread.Start()'s delegate function public string sayHello(string name) {
Possible Duplicate: Finding the Variable Name passed to a Function in C# public new
Possible Duplicate: Convert Month Number to Month Name Function in SQL Hi .... how
Possible Duplicate: java - Array brackets after variable name When writing a Java function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.