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
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
  • 2,113
  • 8
  • 22
3
votes
2 answers

Difference between using non-virtual base class functions versus derived class non-implemented virtual functions

This question is slightly related to What are the differences between overriding virtual functions and hiding non-virtual functions?, but I'm not asking about the technical details, rather about the usage of non-virtual and virtual functions. Here's…
NotAProgrammer
  • 552
  • 1
  • 5
  • 15
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,483
  • 1
  • 20
  • 24
3
votes
2 answers

Why c++ compiler (VS2013) chooses wrong function?

First case #include class A { public: virtual void Write(int i) { std::wcout << L"Write(int) is called" << std::endl; } virtual void Write(wchar_t c) { std::wcout << L"Write(wchar_t) is called" <<…
3
votes
2 answers

How does unqualified name lookup work when using using-declarations?

Is this ill-formed or well-formed according to the c++ standard? namespace M { struct i {}; } namespace N { static int i = 1; } using M::i; using N::i; int main() { sizeof (i); } Clang rejects it and GCC accepts it. According to [namespace.udir-6]…
3
votes
1 answer

Undo name hiding by "using" keyword. Does not work in grandchild class

For example in below program I undo name hiding by "using" keyword. If I have base and one derived class "im getting expected ambiguous call error". But if I have two derived class(child and grand child) now child and grand child having same…
3
votes
1 answer

Implement abstract methods from inherited class

I am trying to do something I haven't really done before. I basically have 3 classes. Class A is an abstract class with pure virtual methods, Class B is a class on it's own that contains methods with the same name as the virtual methods in Class…
user2074102
2
votes
2 answers

Is it well defined to access a variable from an outer scope before it is redefined?

This code compiles with no warnings in gcc-11: int i{ 2 }; { std::cout << i; //prints 2 int i{ 3 }; std::cout << i; //prints 3 } Is this well defined or it just happened to work?
really
  • 93
  • 7
2
votes
2 answers

Name resolution when a structure declaration is hidden by a variable

Let's consider the following demonstrative program. #include struct A { struct B { int b = 10; }; int B = 20; }; template struct C { void f() const { typename /*struct*/ T::B b; …
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
2
votes
2 answers

Function hiding in c++

I was trying out few concepts of function hiding in c++. So here in the derived class I've used scope resolution operator using base::fun to provide scope of base class in derived class. My objective is to print cout<<" base "<< x; but the output…
2
votes
1 answer

Why a method of parent of parent classes' not accessible without of explicit scope resolution?

Let's consider this code: struct message { uint8_t* data; size_t length; }; class device_base { // ... public: virtual ssize_t exec(uint8_t cmd, const uint8_t* data = nullptr, size_t length = 0); inline ssize_t exec(uint8_t cmd, const…
Serge Roussak
  • 1,731
  • 1
  • 14
  • 28
2
votes
2 answers

Java nested scopes and variables' name hiding

I'm learning name look up in Java, and coming from C++ I found interesting that even if Java lets me nest however many blocks of code,I am allowed to hide a name only in the first nested scope: // name hiding-shadowing: local variables hide names in…
Luca
  • 1,658
  • 4
  • 20
  • 41
2
votes
2 answers

Does not name a type error is due to existing enum, but why?

I was just getting this error: "error: ‘Symbol’ does not name a type" I found some other StackOverflow questions talking about circular dependencies, but that is not the case here. In fact I could reproduce it by putting this at the top of the…
Darren Cook
  • 27,837
  • 13
  • 117
  • 217
1
vote
1 answer

C++ enum keyword in function parameters

What is the point of using the enum keyword in the function parameter? It seems to do the same thing without it. enum myEnum{ A, B, C }; void x(myEnum e){} void y(enum myEnum e){} Is there a difference between the two?
TigerFace
  • 13
  • 3