473,405 Members | 2,334 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Overloading problems

Hi,

For some important reasons I've implemented a new type as
a class instead of a struct.

The type is called NKTd and I've overloaded the ==
operator as follows

public static bool operator==( NKTd a, NKTd b )
{
return ( a.value == b.value );
}

Now, if x is a NKTd (or pointer to) and I want to
evaluate something like:

if ( x == null)
{
....
}

The above will invoke the == overloaded code, and I'll
get a runtime error.
Is there a way to 'override' the left side to force it to
look at the reference.

I've tried 'if ( (object)x == null)' and it works fine,
but not very elegant to my opinion.
best regards

Nov 15 '05 #1
3 1037
public static bool operator==( NKTd a, NKTd b )
{
if (a!=null && b!=null) return ( a.value == b.value );
return (object)a==(object)b;
}

Theoretic == is used to know if the two "pointers" point to the same object
If you want to know if two different objects are similar is better to use
(implements) .equals( .. )

<an*******@discussions.microsoft.com> schrieb im Newsbeitrag
news:0f****************************@phx.gbl...
Hi,

For some important reasons I've implemented a new type as
a class instead of a struct.

The type is called NKTd and I've overloaded the ==
operator as follows

public static bool operator==( NKTd a, NKTd b )
{
return ( a.value == b.value );
}

Now, if x is a NKTd (or pointer to) and I want to
evaluate something like:

if ( x == null)
{
...
}

The above will invoke the == overloaded code, and I'll
get a runtime error.
Is there a way to 'override' the left side to force it to
look at the reference.

I've tried 'if ( (object)x == null)' and it works fine,
but not very elegant to my opinion.
best regards

Nov 15 '05 #2
I would use the cast to object, because in essence, that is what you
want to do. You can also make a call to the static ReferenceEquals method
on the Object class, instead of performing the cast.

Also, on a side note, you should override GetHashCode as well, since you
overrode the Equals method (you overloaded the Equals operator, which means
you should have overridden the Equals method as well).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<an*******@discussions.microsoft.com> wrote in message
news:0f****************************@phx.gbl...
Hi,

For some important reasons I've implemented a new type as
a class instead of a struct.

The type is called NKTd and I've overloaded the ==
operator as follows

public static bool operator==( NKTd a, NKTd b )
{
return ( a.value == b.value );
}

Now, if x is a NKTd (or pointer to) and I want to
evaluate something like:

if ( x == null)
{
...
}

The above will invoke the == overloaded code, and I'll
get a runtime error.
Is there a way to 'override' the left side to force it to
look at the reference.

I've tried 'if ( (object)x == null)' and it works fine,
but not very elegant to my opinion.
best regards

Nov 15 '05 #3
public static bool operator==(NKTD p, NKTD z)
{
object tmp = (object)z;
if (tmp == null)
return false;

return (p.Value == z.Value);
}
In one or the other way, one will have to cast it to object, otherwise it will result into
StackOverFlowException, if I use the following code

public static bool operator==(NKTD p, NKTD z)
{
if (tmp == null)
return false;

return (p.Value == z.Value);
}
HTH
Kalpesh

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Doran_Dermot | last post by:
Hi All, I've seen lots of code in which the attributes of a class are accessed and modified using two separate methods. For example: class Problems: def __init__( self, refNum ):...
4
by: Mohammad | last post by:
I'm implementing a c++ template class CScan to minipulate a series of numbers. I have implemented operator() to select a range of numbers it works fine for an expression like: scan1 = scan2(0,...
2
by: bq | last post by:
Hello, This post is really two questions. Question 1: What is the current status on a revision of ISO C++, specifically regarding plans for overloading composite operators? Some people in this...
10
by: pmatos | last post by:
Hi all, I have the following code: class test { public: test(const std::string *n) : name(n) {} virtual ~test() {} const std::string * getName() { return name; }
19
by: jacob navia | last post by:
C++ introduced an interesting feature (among others): operator overloading. The idea is to build a mechanism for the user defining its own number types and the operations to be done with them. ...
39
by: zeus | last post by:
I know function overloading is not supported in C. I have a few questions about this: 1. Why? is it from technical reasons? if so, which? 2. why wasn't it introduced to the ANSI? 3. Is there any...
9
by: Karahan Celikel | last post by:
Here are three simple classes: class A { public void DoIt(B b) { DoSomething(b); } public void DoSomething(B b) {
3
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,...
11
by: jakester | last post by:
I am using Visual C++ 2007 to build the code below. I keep getting linkage error. Could someone please tell me what I am doing wrong? The code works until I start using namespace for my objects. ...
2
by: Colonel | last post by:
It seems that the problems have something to do with the overloading of istream operator ">>", but I just can't find the exact problem. // the declaration friend std::istream &...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
marktang
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,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.