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
0
votes
2 answers

Priority of name hiding in C++ inheritance

Why the call of print() from pointer to base class (Class A) is working and call to print() from Child class object(Class C) is not working? Statement 1: Will give "print A" as output as since A::print() is a virtual function it will call print()…
Gtrex
  • 247
  • 1
  • 7
0
votes
1 answer

Intional use of name hiding for static function inheritance

I have a base class Base and a number of sub classes BaseDerivate1 .. BaseDerivateN. Part of my code is generic and supports any of the sub classes by accessing the used class via a define BASE_DERIVATE. I now need to add a static function that the…
ruhig brauner
  • 943
  • 1
  • 13
  • 35
0
votes
2 answers

this refers to what when a non-overridden method is invoked on a subclass object?

Consider the following code: class Person { String className = "Person"; void printClassName () { System.out.println("I am " + this.className); System.out.println("I am " + this.getClass().getSimpleName()); } } class…
user5818995
0
votes
0 answers

alternatives to name hiding inherited non virtual functions

struct Base { int x; int foo() const { return x; } }; struct Derived : Base { int y; int foo() const { return Base::foo() + y; } }; int main(int argc, char **argv) { Derived d; d.x = 1; d.y = 2; const Base& b = d; …
mkmostafa
  • 3,071
  • 2
  • 18
  • 47
0
votes
1 answer

Replace class and variables names in code snippet

I would like to insert some gist-s of my code in CV. In order not to give idea what this code is about I want to replace all classes, methods and variables names with some random strings automatically (using some script or online creator?), so that…
0
votes
0 answers

C++ - Why does a derived class method void m(const T) override a base class method void m(T)?

Case 1: class T{}; class A { public: virtual void m(T t) { ... } }; class B : public A { public: void m(const T t){ ... } }; int main(){ T t; B b; A* a = &b; a->m(t); } Case 2: class T{}; class A { public: …
Andrea N.
  • 9
  • 1
0
votes
2 answers

C++ Adding overloaded methods for specific template specializations

I have a pretty interesting problem: I have two template classes. One can take any template parameter, the other is more specialized (for this toy problem, we'll say it has to take floating points). template< class T > class CanBeAnything{ /*...*/…
0
votes
1 answer

Name hiding and access base class' non-virtual function (syntax)

In the following code: #include class A { public: void f( float x ) { std::cout << 1; } void g() { std::cout << 11; } }; class B : public A { public: void f( char x ) { std::cout << 2; } void g() { std::cout << 22;…
Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
0
votes
3 answers

Java i/o reading a text while hiding specific words

How would you read the following text while hiding the word SECRET each times it appears ? here is the text : this line has a secret word. this line does not have a one. this line has two secret words. this line does not have any. import…
0
votes
2 answers

c++ How do you call a template class function that is overloaded in the derived class, from the derived class?

Is there a way to overload base template class functions in derived classes, and still be able to call the base class function, as described below? template class testBase { public: void insert() { // Whatever } }; class…
user1394884
  • 211
  • 1
  • 2
  • 6
-1
votes
2 answers

compilation error regarding name hiding ,override and virtual table

I was playing c++ rule. I hit an error but i can't explain it. please help to explain why the compilation error happen. BTW, I am not interesting at fixing the problem. Thanks Q1 why the name hiding doesnt work in the case? for example, if we…
cppython
  • 1,209
  • 3
  • 20
  • 30
1 2 3
4