---
title: "Describe the \"name hiding\" issue in C++ and how it might lead to logical errors."  
description: "Describe the \"name hiding\" issue in C++ and how it might lead to logical errors."  
author: "Sandra Emily"  
published: 2023-08-16  
updated: 2023-08-17  
canonical: https://www.mindstick.com/forum/159543/describe-the-name-hiding-issue-in-c-plus-plus-and-how-it-might-lead-to-logical-errors  
category: "visual c++"  
tags: ["exception", "error", "c++"]  
reading_time: 2 minutes  

---

# Describe the "name hiding" issue in C++ and how it might lead to logical errors.

[Describe](https://www.mindstick.com/interview/12752/what-is-ddms-describe-some-of-its-capabilities) the "[name](https://yourviews.mindstick.com/view/87450/how-to-generate-a-brand-name-using-linkedin-comprehensive-guide) hiding" issue in [C++](https://www.mindstick.com/blog/745/array-in-c-plus-plus) and how it might lead to [logical](https://www.mindstick.com/forum/33581/what-is-inserted-deleted-logical-table-in-sql-server) errors.

## Replies

### Reply by Aryan Kumar

Sure. Name hiding is a C++ concept that occurs when a name is declared in a scope that hides a name with the same name in an outer scope. This can happen with variables, functions, classes, and other entities.

Name hiding can lead to logical errors if the programmer is not aware of it. For example, consider the following code:

C++

```plaintext
class Base {
public:
  void foo() {}
};

class Derived : public Base {
public:
  void foo() {} // This hides the foo() function in Base
};

int main() {
  Base b;
  b.foo(); // This calls the foo() function in Base
  Derived d;
  d.foo(); // This calls the foo() function in Derived
}
```

In this code, the `foo()` function in `Derived` hides the `foo()` function in `Base`. This means that when we call `b.foo()`, we are actually calling the `foo()` function in `Base`. However, when we call `d.foo()`, we are actually calling the `foo()` function in `Derived`.

If the programmer is not aware of name hiding, they might think that the `foo()` function in `Derived` is overriding the `foo()` function in `Base`. However, this is not the case. The `foo()` function in `Derived` is simply hiding the `foo()` function in `Base`.

This can lead to logical errors if the programmer is not careful. For example, if the programmer expects the `foo()` function in `Derived` to call the `foo()` function in `Base`, they might be surprised when it does not.

To avoid name hiding errors, it is important to be aware of the scope of names. When declaring a name, it is important to make sure that the name does not hide a name with the same name in an outer scope.

Here are some tips for avoiding name hiding errors:

- Use fully qualified names whenever possible. This will help to avoid name hiding by making it clear which name you are referring to.
- Use namespaces to organize your code and avoid name collisions.
- Use the `using` directive to bring names into scope. This can be useful if you need to use a name from an outer scope in an inner scope.

By following these tips, you can help to avoid name hiding errors in your C++ code.


---

Original Source: https://www.mindstick.com/forum/159543/describe-the-name-hiding-issue-in-c-plus-plus-and-how-it-might-lead-to-logical-errors

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
