warning ... is hidden in ...
Frits van Bommel
fvbommel at REMwOVExCAPSs.nl
Sat May 3 02:43:59 PDT 2008
Derek Parnell wrote:
> On Fri, 2 May 2008 22:47:33 -0400, Jarrett Billingsley wrote:
>
>> "Derek Parnell" <derek at psych.ward> wrote in message
>> news:1pg84i0mr3ihw$.1ng68jjvixchm$.dlg at 40tude.net...
>>> Using dmd 2.013 I'm now getting some messages along the lines of ...
>>>
>>> warning: class Foo Object opCmp is hidden in Foo
>>>
>>> What does this actually mean and how can I remove the warning (without
>>> removing the -w switch)?
>>>
>> It means that Object.opCmp has the signature "int(Object)" whereas your
>> class's opCmp has the signature "int(<something other than Object>)". In
>> other words, you're hiding the base class implementation of opCmp.
>
> "hiding"?! Isn't one of the features of OO the ability to create
> specialized methods that override the parent class' method of the same
> signature?
Yes, but to do that the signatures need to match. In this case, your
opCmp should take any Object (not just your specific subclass of it) and
return an int. Or at least, there should be an opCmp having such a
signature (i.e. it doesn't have to be the *only* opCmp overload).
>> Try either putting 'override' on your implementation of opCmp,
>
> Tried that and got "function Foo.opCmp does not override any function"
This one only works to force you to get the signature right.
>> or doing "alias super.opCmp opCmp" inside the class declaration.
>
> Tried that and I got "basic type expected, not super"
Try substituting 'super' by the class you directly inherit from. (That's
Object unless you mentioned one explicitly)
However, this will mean that when e.g. an object of your class is
compared to an Object or while the static type is Object the inherited
method will be called. Which would normally be a bad thing for opCmp.
> Any other suggestions gladly accepted ;-)
Presumably your code currently looks something like:
---
class C {
int opCmp(C c) {
// implementation
}
}
---
If you want to get rid of the warning, try one of these:
---
class C {
/// Overrides Object.opCmp, throws if the argument is not a C.
override int opCmp(Object o) {
if (C c = cast(C) o)
return opCmp(c);
throw new Exception("Can't compare C to " ~ o.classinfo.name);
}
int opCmp(C c) {
// implementation
}
}
---
or
---
class C {
override int opCmp(Object o) {
if (C c = cast(C) o) {
// implementation
}
throw new Exception("Can't compare C to " ~ o.classinfo.name);
}
}
---
The second one is essentially the inlined version of the first, but will
be less efficient when the a and b in "a < b" are statically known to be
instances of C.
And both versions assume the argument isn't null; if you want to handle
null input (not sure why you would) you'll need some more checking.
More information about the Digitalmars-d-learn
mailing list