I am creating a class with operator overloads. It makes references to another
class I've created, but do not wish to modify. I try to overload an operator
having arguments having the other class type, but none of the new class type.
However it does return a type of the new class. The compiler tells me that at
least one parameter must be of the containing type. Is there a way to do
this? The other class hos no reference to this new class. Is there a way to
overload operators in the other class within the definition for the new
class? The overload I'm trying to create does not conflict with existing
overloads in the other class. 5 2043
bsaucer
It sounds like you created two classes, correct?
And you want to create an overloaded operator that expects one class &
returns the second, correct?
Have you tried:
' VB.NET 2005 syntax
Public Class FirstClass
End Class
Public Class OtherClass
Public Shared Operator +(ByVal otherClass1 As OtherClass, ByVal
otherClass2 As OtherClass) As FirstClass
End Operator
End Class
Note the overloaded operator is in the class that the parameters are, not
the class of the return type.
Hope this helps
Jay
"bsaucer" <bs*****@discussions.microsoft.com> wrote in message
news:59**********************************@microsof t.com... I am creating a class with operator overloads. It makes references to another class I've created, but do not wish to modify. I try to overload an operator having arguments having the other class type, but none of the new class type. However it does return a type of the new class. The compiler tells me that at least one parameter must be of the containing type. Is there a way to do this? The other class hos no reference to this new class. Is there a way to overload operators in the other class within the definition for the new class? The overload I'm trying to create does not conflict with existing overloads in the other class.
The problem is, the "other class" (written first) has no "knowledge" of the
new class. The new class does have knowledge of the older class.
Theoretically, the old class is "off limits", as if it were written by
someone else while I develop the new class.
I have found a workaround: I used a "widening ctype" operator in my new
class to allow the calling routine to use arguments of the other class, so I
no longer need to to overload all my arithmetic operators specifying the
other arguments.
"Jay B. Harlow [MVP - Outlook]" wrote: bsaucer It sounds like you created two classes, correct?
And you want to create an overloaded operator that expects one class & returns the second, correct?
Have you tried:
' VB.NET 2005 syntax
Public Class FirstClass End Class
Public Class OtherClass Public Shared Operator +(ByVal otherClass1 As OtherClass, ByVal otherClass2 As OtherClass) As FirstClass End Operator End Class
Note the overloaded operator is in the class that the parameters are, not the class of the return type.
Hope this helps Jay
"bsaucer" <bs*****@discussions.microsoft.com> wrote in message news:59**********************************@microsof t.com...I am creating a class with operator overloads. It makes references to another class I've created, but do not wish to modify. I try to overload an operator having arguments having the other class type, but none of the new class type. However it does return a type of the new class. The compiler tells me that at least one parameter must be of the containing type. Is there a way to do this? The other class hos no reference to this new class. Is there a way to overload operators in the other class within the definition for the new class? The overload I'm trying to create does not conflict with existing overloads in the other class.
bsaucer, The problem is, the "other class" (written first) has no "knowledge" of the new class.
Ah! there's the rub!
I have found a workaround: I used a "widening ctype" operator in my new
Just be aware that a "widening ctype" operator may cause implicit
conversions when you do least expect them! (for example during an assignment
or being passed as a parameter to another method (other then one of your
overloaded operators).
If possible I would modify the "other class"! to avoid the implicit
conversions!
For example:
The DateTime operator - returns a TimeSpan, TimeSpan does not have an
implicit ("widening ctype") from DateTime. As a "widening ctype" on TimeSpan
from DateTime could cause lose of the Date part of the DateTime. TimeSpan
could have a narrowing ctype as TimeSpan represents less data then
DateTime...
NOTE: There are times when implicit conversions do make sense, such as an
implicit conversion from Double to Complex (where Complex is a type that
represents a complex number).
Hope this helps
Jay
"bsaucer" <bs*****@discussions.microsoft.com> wrote in message
news:C0**********************************@microsof t.com... The problem is, the "other class" (written first) has no "knowledge" of the new class. The new class does have knowledge of the older class. Theoretically, the old class is "off limits", as if it were written by someone else while I develop the new class.
I have found a workaround: I used a "widening ctype" operator in my new class to allow the calling routine to use arguments of the other class, so I no longer need to to overload all my arithmetic operators specifying the other arguments.
"Jay B. Harlow [MVP - Outlook]" wrote:
bsaucer It sounds like you created two classes, correct?
And you want to create an overloaded operator that expects one class & returns the second, correct?
Have you tried:
' VB.NET 2005 syntax
Public Class FirstClass End Class
Public Class OtherClass Public Shared Operator +(ByVal otherClass1 As OtherClass, ByVal otherClass2 As OtherClass) As FirstClass End Operator End Class
Note the overloaded operator is in the class that the parameters are, not the class of the return type.
Hope this helps Jay
"bsaucer" <bs*****@discussions.microsoft.com> wrote in message news:59**********************************@microsof t.com... >I am creating a class with operator overloads. It makes references to >another > class I've created, but do not wish to modify. I try to overload an > operator > having arguments having the other class type, but none of the new class > type. > However it does return a type of the new class. The compiler tells me > that > at > least one parameter must be of the containing type. Is there a way to > do > this? The other class hos no reference to this new class. Is there a > way > to > overload operators in the other class within the definition for the new > class? The overload I'm trying to create does not conflict with > existing > overloads in the other class.
Your note describes the situation, where an object of the original class CAN
be "upgraded" to the new class. I was going to use that anyway. I was just
surprised that it enabled me to eliminate several "redundant" operator
overloads! NOTE: There are times when implicit conversions do make sense, such as an implicit conversion from Double to Complex (where Complex is a type that represents a complex number).
Note that the double class has no "knowledge" of complex numbers, and
therefore makes no references to the complex class. However, the complex
class would make heavy use of doubles. You shouldn't have to modify the class
of doubles in any way to accomodate complex numbers.
bsaucer, Your note describes the situation, where an object of the original class CAN be "upgraded" to the new class.
I don't follow what you mean by "CAN be "upgraded"", do you mean an implicit
conversion is "well defined" between the two classes?
Note that the double class has no "knowledge" of complex numbers, and
Correct, the double class has *NO* knowledge of complex numbers! You add or
subtract (or any other operator) two double numbers together and you still
have a double!
However defining an implicit conversion from Double to Complex makes sense
as its "well defined".
Where as in the case of DateTime if you subtract two DateTimes you have a
TimeSpan, its doesn't completely make sense to define the result of
subtracting two DateTimes in the terms of a DateTime (as you actually have a
duration & not an actual DateTime, hence the TimeSpan type)...
Likewise implicitly converting a DateTime to or from a TimeSpan does not
really make sense either.
NOTE: I'm not saying its wrong to have the implicit conversion, I'm just
saying make sure the implicit conversion makes sense in all cases... Those
cases are, but not limited to, assignments & parameter passing.
Hope this helps
Jay
"bsaucer" <bs*****@discussions.microsoft.com> wrote in message
news:9D**********************************@microsof t.com... Your note describes the situation, where an object of the original class CAN be "upgraded" to the new class. I was going to use that anyway. I was just surprised that it enabled me to eliminate several "redundant" operator overloads!
NOTE: There are times when implicit conversions do make sense, such as an implicit conversion from Double to Complex (where Complex is a type that represents a complex number).
Note that the double class has no "knowledge" of complex numbers, and therefore makes no references to the complex class. However, the complex class would make heavy use of doubles. You shouldn't have to modify the class of doubles in any way to accomodate complex numbers.
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Shaun |
last post by:
Hi,
I'm trying to overload the divide operator in python for basic arithmetic.
eg. 10/2 ... no classes involved.
I am attempting to redefine operator.__div__ as follows:
# my divide...
|
by: Chris |
last post by:
To me, this seems rather redundant. The compiler requires that if you
overload the == operator, you must also overload the != operator. All I do
for the != operator is something like this:
...
|
by: bsaucer |
last post by:
I am creating a class with operator overloads. It makes references to another
class I've created, but do not wish to modify. I try to overload an operator
having arguments having the other class...
|
by: Tony |
last post by:
I have an operator== overload that compares two items and returns a new
class as the result of the comparison (instead of the normal bool)
I then get an ambiguous operater compile error when I...
|
by: raylopez99 |
last post by:
I need an example of a managed overloaded assignment operator for a
reference class, so I can equate two classes A1 and A2, say called
ARefClass, in this manner: A1=A2;. For some strange reason...
|
by: Norman Diamond |
last post by:
In an antique obsolete version of MFC, a CString expression could be
subscripted in order to retrieve one element.
Visual Studio 2005 defines CSimpleStringT::operator. At first glance it
looks...
|
by: y-man |
last post by:
Hi,
I am trying to get an overloaded operator to work inside the class it
works on. The situation is something like this:
main.cc:
#include "object.hh"
#include "somefile.hh"
object obj,...
|
by: phiefer3 |
last post by:
I'm currently a student, but this problem isn't directly related to what I have to do on an assignment. It's just a problem I've had with some supporting features.
First of all, I'm using MSVS...
|
by: rn5a |
last post by:
I use VB.NET to create ASP.NET apps. If I am not wrong, there is
something called method overloading in VB.NET (like in C#) which is
one of the features in OOP (polymorphism) but does VB.NET also...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |