Operators Inherited?

  • Thread starter Thread starter Shak
  • Start date Start date
S

Shak

Hi all.

I was led to believe that static methods were not inherited by their
subclasses (and since that makes sense, rightly so).

However, a subclass I've written is using it's (abstract) superclass's
operators. How can that be when operators are always static?

Is it a special case? It's handy, sure, but doesn't it "break" the language
somehow? Or isn't this actually inheritance at play here?

Shak
 
Shak said:
I was led to believe that static methods were not inherited by their
subclasses (and since that makes sense, rightly so).

Huh? Why would you think that? The only thing special about static
methods is that they don't get a "this" reference. Public and internal
static members are just as visible to derived classes as they are to
totally unrelated classes; protected static members are just as
visible to derived classes as protected instance members.
 
Shak said:
I was led to believe that static methods were not inherited by their
subclasses (and since that makes sense, rightly so).

They can't be overridden, so they're not inherited in the same way as
virtual methods are, but the whole namespace of the ancestor type is
merged into the descendant type, so there is a kind of inheritance of
scope.
However, a subclass I've written is using it's (abstract) superclass's
operators. How can that be when operators are always static?

When you use an operator on two custom types, the compiler examines both
types' scopes, looking for operator overload definitions. So, if you've
got two types, Foo and Bar, with values foo and bar:

foo Op bar

.... it'll look in the namespaces of both Foo and Bar for static methods
called op_Op (where 'Op' is Add, Subtract, etc.) for which there exist
overloads that can accept values of type (Foo, Bar). That 'accept'
includes polymorphism by way of inheritance, of course.

Now, static methods don't get inherited in the sense of a virtual
instance method - but because the whole namespace is inherited, all the
public static methods that are visible in the ancestor are also visible
in the descendant. Thus, the ancestor's operators also work on its
descendants.

-- Barry
 
Jon Shemitz said:
Huh? Why would you think that? The only thing special about static
methods is that they don't get a "this" reference. Public and internal
static members are just as visible to derived classes as they are to
totally unrelated classes; protected static members are just as
visible to derived classes as protected instance members.


Perhaps "inherited" was the wrong word. What I meant was that if a
superclass has a public static void method GetInt(), you would not be able
to call that method on a subclass - ie you can't mark them virtual.

My initial impression was that operators were just convienince for regular
static methods. So:

public static bool operator==(A one, A two), was equivalent to

public static bool myEquals(A one, A two).

They're not though, since, where B is a subclass of A:

B b1 = new B();
B b2 = new B();

bool result = (b1==b2); //compiles and uses the impl of operator== in A,
bool result2 = b1.Equals(b2); //doesn't compile, obviously.

I've since read after posting the OP that operators aren't just static
methods, and further what is to be called gets determined at compile time.
So, not only do you get access to any operators in the superclass, but you
may even get to use an operator that hasn't been explicity declared for your
type (like above with b1 == b2), which doesn't occur with regular virtual
methods (unless you cast).

Shak
 
Barry Kelly said:
They can't be overridden, so they're not inherited in the same way as
virtual methods are, but the whole namespace of the ancestor type is
merged into the descendant type, so there is a kind of inheritance of
scope.

Is that just the case for operators?
When you use an operator on two custom types, the compiler examines both
types' scopes, looking for operator overload definitions. So, if you've
got two types, Foo and Bar, with values foo and bar:

foo Op bar

... it'll look in the namespaces of both Foo and Bar for static methods
called op_Op (where 'Op' is Add, Subtract, etc.) for which there exist
overloads that can accept values of type (Foo, Bar). That 'accept'
includes polymorphism by way of inheritance, of course.

Again, is this just the case for operators?
Now, static methods don't get inherited in the sense of a virtual
instance method - but because the whole namespace is inherited, all the
public static methods that are visible in the ancestor are also visible
in the descendant. Thus, the ancestor's operators also work on its
descendants.

But if that's so, why can't you call a superclass's static method from its
subclass (like I try to do in my other post)?

Shak
 
Shak said:
But if that's so, why can't you call a superclass's static method from its
subclass (like I try to do in my other post)?

Ooops, seems like you can after all and I'm talking rubbish! My mistake...
Disregard.

Thanks for the help guys!

Shak
 
Back
Top