Questions tagged [name-hiding]

A feature of the C++ language that causes functions in a base class to be hidden by overloads in a derived class.

A name can be hidden by another declaration with the same name in a different scope.

This can happen with names in nested scopes:

void f(int);

namespace ns {
  void f();     // hides ::f(int)
  void g() {
    f(1);       // error, name lookup finds ns::f()
  }
}

And also with names in derived classes:

class Base {
public:
  int func(int);
};
class Derived : Base {
public:
  void func();  // hides Base::func(int)
};

Derived d;
int i = d.func(1);  // error, name lookup finds Derived::func()

A hidden name can be made visible by re-declaring it in the second scope with a using declaration.

56 questions
15
votes
1 answer

Template parameter name hiding

I got recently bitten by (simplified) struct Base { typedef char T; }; template struct Foo : Base { T x[50]; // This is Base::T, not the template parameter }; In other words a class member name hides a template parameter (even…
6502
  • 112,025
  • 15
  • 165
  • 265
13
votes
3 answers

Why class member functions shadow free functions with same name?

It recently came to my attention that member functions completely shadow free functions with the same name when inside the class. And by completely I mean that every free function with the same name is not considered for overload resolution at all.…
yuri kilochek
  • 12,709
  • 2
  • 32
  • 59
10
votes
2 answers

Overloading a super class's function

Is there something in the C++ standard that prevents me from overloading a super class's function? Starting with this pair of classes: class A { // super class int x; public: void foo (int y) {x = y;} // original…
chrisaycock
  • 36,470
  • 14
  • 88
  • 125
7
votes
5 answers

Warning: overloaded virtual function "Base::process" is only partially overridden in class "derived"

I am getting below warning . part of my code is : class Base { public: virtual void process(int x) {;}; virtual void process(int a,float b) {;}; protected: int pd; float pb; }; class derived: public Base{ public: void…
Ashwin
  • 411
  • 1
  • 10
  • 28
7
votes
4 answers

Confusion regarding name hiding and virtual functions

Refering another so question Consider the code : class Base { public: virtual void gogo(int a){ printf(" Base :: gogo (int) \n"); }; virtual void gogo(int* a){ printf(" Base :: gogo (int*) \n"); }; }; class…
hiteshg
  • 81
  • 1
  • 5
6
votes
2 answers

Variable name same as function name giving compiler error... Why?

Ran into an interesting issue today and am trying to understand why. Consider the following: class Base { public: Base(){} ~Base(){} static void function1(){} void function2() { int function1; …
5
votes
0 answers

How to make all hidden names from a base class accessible in derived one?

Starting from this question: Pointer derived from pure virtual class(A) can't access overload method from the pure class (B) And considering this simplified code: #include #include class Abstract { public: virtual void…
j4x
  • 3,595
  • 3
  • 33
  • 64
5
votes
1 answer

Pointer derived from pure virtual class(A) can't access overload method from the pure class (B)

Consider I have two pure virtual classes, one deriving from the another and a concrete class deriving from the last mentioned: #include #include class Abstract1 { public: virtual ~Abstract1() { }; virtual void…
Paiusco
  • 305
  • 1
  • 14
5
votes
2 answers

Inheriting templated operator= in C++14: different behaviour with g++ and clang++

I have this code which works as expected with GCC 9.1: #include template< typename T > class A { protected: T value; public: template< typename U, typename..., typename = std::enable_if_t<…
5
votes
3 answers

C++ inheritance and name hiding

I know this is not the first question on this subject, but all the other related questions (and answers) I read were slightly out of the point, according to me. Take the code #include using namespace std ; class Base { public: void…
user2335321
  • 59
  • 1
  • 4
4
votes
1 answer

Variadic function Overloading and SFINAE - Solve ambiguity to simulate "hide by signature"

I'd like to have "hiding by signature" instead of "hiding by name" in c++. So I wrote a macro which defines a variadic function that delegates all calls to it's base class if some exists. I can't use a using declaration because I don't want it to…
Bernd
  • 2,113
  • 8
  • 22
4
votes
1 answer

function template cannot hide class name?

This works in GCC and Comeau: struct X {}; void X() {} This breaks in Comeau: struct X {}; template< typename T > void X() {} This breaks both: template< typename T > struct X {}; template< typename T > void X() {} The rule is defined by…
Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
4
votes
2 answers

C++ Nested Scope Accessing

I recently saw this code in cppreference: string str="global scope"; void main() { string str="main scope"; if (true){ string str="if scope"; cout << str << endl; } cout << str << endl; } Which outputs: if…
J3STER
  • 1,027
  • 2
  • 11
  • 28
4
votes
3 answers

Access member field with same name as local variable (or argument)

Consider following code snippet: struct S { S( const int a ) { this->a = a; // option 1 S::a = a; // option 2 } int a; }; Is option 1 is equivalent to option 2? Are there cases when one form is better than another? Which…
αλεχολυτ
  • 4,792
  • 1
  • 35
  • 71
4
votes
4 answers

Name hiding in constructor initialization list

I want to modify a constructor to use an initialization list as in the following example: class Foo { public: Foo(std::wstring bar); private: std::wstring bar; }; // VERSION 1: Foo::Foo(std::wstring bar) {this->bar = bar} // VERSION…
JBentley
  • 6,099
  • 5
  • 37
  • 72
1
2 3 4