Gary Labowitz said:
andre dajd said:
Tron Thomas said:
Under the right compiler the following code: [snip]
This issue with methods being hidden does not exist in other languages
such as Java.
That's because in Java all methods are virtual and there is no multiple
inheritance, hence the abovementioned tradeoff did not appear relevant to
Java designers.
Also note that in Java the two functions are considered different if their
signatures are different, i.e. if the name plus the number and type of forma
l parameters differ.
Same in C++.
In this case if you called Method on the derived class
passing it an int (derived.Method(int)) you will run the inherited base
class Method; if you call Method on the derived class passing it a float
(derived.Method(float)) you will run the derived class Method. This is
called method overloading.
Again, same in C++, once you have brought the superclass' method into
the scope of the child; there will be no ambiguity in C++. But the
point here is different.
Overloading a function always means having a collection of
(different, obviousely) functions with same name, but different
signatures. Methods are special case of functions and you can
overload them by either extending a class or implementing several
overloaded members in the same class.
The only difference is what happens if you indeed overload a member in
a child class. Java will always differentiate it from all overloads
pulled from the superclass, overriding the one with same signature, if
it were present in the superclass. C++ will do same, once
specifically instructed by the developer by
"using" directive. C++ simply does not do it automatically for the
reasons I have already mentioned. Otherwise, the parent methods will
be hidden. This does not mean that the superclass method and
overriding method were considered "same". They are not same and
that's why the compiler issues the warning reported in the root
message. It's only the C++ default name resolution rule.
Java will not convert the int call to a float and
call the derived class method. This is a major difference between C++ and
Java.
Well, yes, Java is more strict in automatic conversion, but this is
not the point here. The point is that you may still bring several
overloaded methods into a leaf class; in Java you will have to do
repeated class inheritance while in C++ you can do it in one shot.
Then, if in the leaf class you may find yourself calling (by mistake)
a method from a superclass and then spend a lot of time figuring out
what went wrong. Note, that you will have to do that anyway even if
you do not overload a function in the child; you may still mix up the
parent members.
Apparently, this featuers comes from the time when nagivating thorugh
the class hierarchy was quite a complex task, which it was in the
absense of Javadoc or Doxygen or advanced IDEs, or anything else
alike. So, this C++ feature simply warns the developer about a
possible problem.
d