Inheritence and overloading operators

N

Noodle

Hi Guys, not sure if anyone can help me on this one but......

I have a class that I overload the equality operator (=). I also have
a class that inherits from this first class which has extra attributes
that i need to check to see if they are equal for the previous
overload to work correctly. No my question is how do I do this? The
obvious answer is to overload the overloaded operator and then copy
the comparison routine from the inherited object and copy it to the
new routine in the derived class, then add any extra comparisons.

although this will work for me, it adds the posibility that the base
class will be updated and derived classes that inherit from it will
not have a working comparison check as the new code added to the
original class will need to be copied to the new one.

Now I know that I can use mybase to access the base object of an
inherited object but as the overloaded function takes in two arguments
of type (derived class) how can I access the base class to do the
comparison.

Thanks in advance,

Dean.
 
P

Phill W.

Noodle said:
I have a class that I overload the equality operator (=). I also have
a class that inherits from this first class which has extra attributes
that i need to check to see if they are equal for the previous
overload to work correctly. No my question is how do I do this? The
obvious answer is to overload the overloaded operator and then copy
the comparison routine from the inherited object and copy it to the
new routine in the derived class, then add any extra comparisons.

I think you mean /Overridden/, not Overloaded.
You override methods in base classes where the arguments are the same,
but you want them to do something different
You Overload methods (in the same class or its Superclass) when you want
to do the same /sort/ of thing, but supplying different arguments.
although this will work for me, it adds the posibility that the base
class will be updated and derived classes that inherit from it will
not have a working comparison check as the new code added to the
original class will need to be copied to the new one.

So ask the base class if /it/ think things are the same, based on /its/
view of the world and, if it says that every thing's OK, then "second
guess" it, based on the subclasses view of things. (I haven't done
overloaded operators yet, but here's the same idea with functions):

Class Super
Public Property X() as ...
Public Property Y() as ...

Public Overridable Function Equals( _
other as Super _
) as Boolean
If Me.X <> other.X Then
Return False
End If
If Me.Y <> other.Y Then
Return False
End If
Return True
End Function
End Class

Class Subb
Public Property Z() as ...

Public Overrides Function Equals( _
other as Subb _
) as Boolean
If Not MyBase.Equals( other ) Then
Return False
End If

If Me.Z <> other.Z Then
Return False
End If

Return True
End Function
End Class

HTH,
Phill W.
 
N

Noodle

I think you mean /Overridden/, not Overloaded.
You override methods in base classes where the arguments are the same,
but you want them to do something different
You Overload methods (in the same class or its Superclass) when you want
to do the same /sort/ of thing, but supplying different arguments.


So ask the base class if /it/ think things are the same, based on /its/
view of the world and, if it says that every thing's OK, then "second
guess" it, based on the subclasses view of things. (I haven't done
overloaded operators yet, but here's the same idea with functions):

Class Super
Public Property X() as ...
Public Property Y() as ...

Public Overridable Function Equals( _
other as Super _
) as Boolean
If Me.X <> other.X Then
Return False
End If
If Me.Y <> other.Y Then
Return False
End If
Return True
End Function
End Class

Class Subb
Public Property Z() as ...

Public Overrides Function Equals( _
other as Subb _
) as Boolean
If Not MyBase.Equals( other ) Then
Return False
End If

If Me.Z <> other.Z Then
Return False
End If

Return True
End Function
End Class

HTH,
Phill W.

Hi Phill, yeah I meant overrides (doh). What you've suggested wont
work as to overload an operator you have to have arguments for each
class at the side of the operator in the expression.

eg -

Public Shared Operator =(ByVal olhs As CampaignDeal, ByVal orhs As
CampaignDeal) As Boolean


This means that I do not have access to the mybase functionality for
the passed in arguments. I have figured it out now anyway, to compare
the base class I do a directcast of the two arguments to the inherited
class type and compare those, then if they equal start checking any
attributes that the derived class has extra.
 
N

Noodle

Hi Phill, yeah I meant overrides (doh). What you've suggested wont
work as to overload an operator you have to have arguments for each
class at the side of the operator in the expression.

eg -

Public Shared Operator =(ByVal olhs As CampaignDeal, ByVal orhs As
CampaignDeal) As Boolean

This means that I do not have access to the mybase functionality for
the passed in arguments. I have figured it out now anyway, to compare
the base class I do a directcast of the two arguments to the inherited
class type and compare those, then if they equal start checking any
attributes that the derived class has extra.- Hide quoted text -

- Show quoted text -

damn done it again, i mean override :(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top