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 2061
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*****@discus sions.microsoft .com> wrote in message
news:59******** *************** ***********@mic rosoft.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*****@discus sions.microsoft .com> wrote in message news:59******** *************** ***********@mic rosoft.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*****@discus sions.microsoft .com> wrote in message
news:C0******** *************** ***********@mic rosoft.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*****@discus sions.microsoft .com> wrote in message news:59******** *************** ***********@mic rosoft.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*****@discus sions.microsoft .com> wrote in message
news:9D******** *************** ***********@mic rosoft.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 function
def safediv(a,b):
return ...
|
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:
public static bool operator !=(MyType x, MyType y)
{
return !(x == y);
}
That way the == operator handles everything, and extra comparing logic isn't
|
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 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...
|
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 attempt to check to
see if the object is null:
"The call is ambiguous between the following methods or properties:
'TestObject.operator ==(TestObject, string)' and 'TestObject.operator
==(TestObject, TestObject)"
Does anyone have any idea how to...
|
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 my C++.NET
2.0 textbook does not have one. I tried to build one using the format
as taught in my regular C++ book, but I keep getting compiler errors.
Some errors claim (contrary to my book) that you cannot use a static
function, that you must...
| |
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 like it might have been intended to provide backwards compatibility
for antique programs. But there seems to be no way to use it.
CString s = _T("ab");
short i = 1;
_TCHAR c = s;
|
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, obj2 ;
|
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 2005 standard edition. I haven't downloaded or acquired any additional libraries or tools, just what gets installed with the studio by default. Also, I'm using C++ and started the problem projects as an empty CLR project.
Currently I've had several...
|
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 include
operator overloading like C# does?
Ron
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |