Why isn't that a compilation error? - define a variable in a subclass with the same name of a superclass method
Ali Çehreli
acehreli at yahoo.com
Thu Nov 10 16:17:11 UTC 2022
On 11/10/22 07:02, Hipreme wrote:
> On Thursday, 10 November 2022 at 14:30:30 UTC, Steven Schveighoffer
wrote:
>> When you derive a type, and override a name, the old name is masked or
>> hidden.
Yes, this is know as "name hiding" and well known in C++ circles. It
hides not only functions but member variables as well.
> That could trigger an error or at least a warning, just silently hiding
> that can lead to bugs
The opposite can cause bugs as well and that's why such a feature
exists: Imagine a case where foo(42) call was being dispatched to the
subclass's foo(double) member function. Without your knowledge, the base
class adds a foo(int) to their existing foo(string). Now your foo(42)
call would silently be dispatched to the base class function.
Name hiding makes it explicit where you want your calls go to.
Ali
More information about the Digitalmars-d
mailing list