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.

52 questions
14
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
  • 104,192
  • 14
  • 145
  • 251
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
  • 32,202
  • 12
  • 79
  • 116
10
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
  • 11,212
  • 2
  • 25
  • 52
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
  • 391
  • 1
  • 7
  • 27
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,339
  • 2
  • 25
  • 54
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
  • 188
  • 12
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<…
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
  • 1,589
  • 6
  • 16
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
  • 126,977
  • 21
  • 238
  • 404
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,408
  • 1
  • 23
  • 62
4
votes
3 answers

Confusion over virtual functions and derived classes

I am trying to understand the following bit of code: #include using namespace std; class Base { public: virtual void f(float) { cout << "Base::f(float)\n"; } }; class Derived : public Base { public: virtual void…
3
votes
4 answers

C++ using declaration for parameter pack

I would like to define a class which inherits from a bunch of classes but which does not hide some specific methods from those classes. Imagine the following code: template class SomeClass : public Bases... { public: using…
Bernd
  • 1,589
  • 6
  • 16
3
votes
2 answers

Haskell modules: hidden names and ghci

I'm trying to export just a subset of names from a Haskell module, but ghci happily lets me access even the hidden names. module Hiding (shown, calc) where calc = shown * hidden shown :: Int shown = 3 hidden :: Int hidden = 2 But when trying…
oggy
  • 3,225
  • 1
  • 17
  • 24
1
2 3 4