473,761 Members | 3,542 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Testing for instance of object when operator overloaded

Hi,

I've made a class with the following operator overloads listed below.
However, If I test a 'pointer' for an instace, I get an exception that the
object is not set to an insstance of an object.

ID id = new ID(1,2)
.....
e.g. if ( id != null ) <- this line calls the static overload, I guees,
instead of testing whether the id is set to an object or not. How do I test
for an instance on id, do I have to test in another way, or are my overloads
written in a wrong way?

thanks.
public static bool operator==( ID a, ID b )
{
return ( a.no == b.no && a.rev == b.rev );
}

public static bool operator!=( ID a, ID b )
{
return ( !(a.no == b.no && a.rev == b.rev) );
}
Jan 11 '06 #1
3 1445
Jesper wrote:
I've made a class with the following operator overloads listed below.
However, If I test a 'pointer' for an instace, I get an exception that the
object is not set to an insstance of an object.

ID id = new ID(1,2)
....
e.g. if ( id != null ) <- this line calls the static overload, I guees,
instead of testing whether the id is set to an object or not. How do I test
for an instance on id, do I have to test in another way, or are my overloads
written in a wrong way?


You need to check for a and b being null in your overloaded operators.
In order to perform that check, you'll need to use
Object.Referenc eEquals or
cast the parameters to object before testing against null, otherwise
you'll just recurse.

Let me know if this isn't enough detail to get you going.

(You might want to implement != by just return !(a==b) by the way, to
keep all your common logic in one place.)

Jon

Jan 11 '06 #2
Thanks Jon!.
Regards Jesper, DK.

"Jon Skeet [C# MVP]" wrote:
Jesper wrote:
I've made a class with the following operator overloads listed below.
However, If I test a 'pointer' for an instace, I get an exception that the
object is not set to an insstance of an object.

ID id = new ID(1,2)
....
e.g. if ( id != null ) <- this line calls the static overload, I guees,
instead of testing whether the id is set to an object or not. How do I test
for an instance on id, do I have to test in another way, or are my overloads
written in a wrong way?


You need to check for a and b being null in your overloaded operators.
In order to perform that check, you'll need to use
Object.Referenc eEquals or
cast the parameters to object before testing against null, otherwise
you'll just recurse.

Let me know if this isn't enough detail to get you going.

(You might want to implement != by just return !(a==b) by the way, to
keep all your common logic in one place.)

Jon

Jan 11 '06 #3
Jesper wrote:
Hi,

I've made a class with the following operator overloads listed below.
However, If I test a 'pointer' for an instace, I get an exception that the
object is not set to an insstance of an object.

ID id = new ID(1,2)
....
e.g. if ( id != null ) <- this line calls the static overload, I guees,
instead of testing whether the id is set to an object or not. How do I test
for an instance on id, do I have to test in another way, or are my overloads
written in a wrong way?

thanks.
public static bool operator==( ID a, ID b )
{
return ( a.no == b.no && a.rev == b.rev );
}

public static bool operator!=( ID a, ID b )
{
return ( !(a.no == b.no && a.rev == b.rev) );
}


Here is the "template" I follow when I oveload op==:

class ID
{
public override bool Equals(object obj)
{
ID rhs = obj as ID;
if(rhs == null)
return false;
return this.no == rhs.no && this.rev == rhs.rev;
}
public override int GetHashCode()
{
return no.GetHashCode( ) ^ rev.GetHashCode ();
}
public static bool operator ==(ID lhs, ID rhs)
{
return object.Equals(l hs, rhs);
}
public static bool operator !=(ID lhs, ID rhs)
{
return !object.Equals( lhs, rhs);
}
private int no, rev;
}

In this way i have all the logic in the Equals() method which is getting
called from the operators.

HTH,
Andy
--
To email me directly, please remove the *NO*SPAM* parts below:
*NO*SPAM*xmen40 @*NO*SPAM*gmx.n et
Jan 12 '06 #4

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

Similar topics

6
3258
by: Shabam | last post by:
A web application of mine developed using C# + MS SQL runs fine normally. However when I stress test it with a load testing software (using about 60 simultaneous users) some instances start erroring out. I see two different errors. One is a "Object reference not set to an instance of an object." error, which appears to always contain the same information, and the other is a "There is no row at position X.", where X is a number. Is this...
3
1615
by: matt p | last post by:
example: FunClass myfun; FunClass *lotsofunptr=&myfun; myfun; //calls the overloaded operator; lotsofunptr->;//error help is much apreciated
8
3528
by: SpOiLeR | last post by:
Hello! I have a matrix class like this: class MyObject; // MyMatrix is contains MyObjects class MyMatrix { public: ...
4
1267
by: EL OSO | last post by:
Hi, let's suppose I have a class A { int x; ... public static bool operator == (A a1 , A a2) { return (a1.x==a2.x); }
7
1581
by: Genival Carvalho | last post by:
Hello friends... Inside my class how do to use explicit using Overloaded operator or use default ? Ex: Dim x as Integer = A + B ' Use default + operator Dim OV as integer = X + Z ' I will need use Overloaded + Operator ??? Thanks and sorry my bad English.
5
1645
by: shaun roe | last post by:
I am about to replace a plain function which returns a string with an object; I am trying to preserve the interface to clients of the function, while imbuing the function with some internal state. lets say the function is string foo(int myInt); I thought to make an object with constructor f(int myInt) and hide the default constructor, copy, assignment (anything else?), but provide some
13
7276
by: Karthik | last post by:
In a recent techincal interview on C++, I came across this strange question.... How to ensure that your class object is always created in heap (by a new operator)? The compiler should throw error, if the object is created in stack. Note:- Dont hide the construtor. Is it possible?
2
7375
by: B. Williams | last post by:
I have an assignment for school to Overload the operators << and >and I have written the code, but I have a problem with the insertion string function. I can't get it to recognize the second of number that are input so it selects the values from the default constructor. Can someone assist me with the insertion function. The code is below. // Definition of class Complex #ifndef COMPLEX1_H
1
2542
by: subramanian100in | last post by:
Consider the following: int x; int y; int z; (x+y) = z; For this statement, I get the following error with g++ compiler: error: non-lvalue in assignment Suppose I have a class Test and x, y, z are objects of type Test.
0
9554
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, 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...
0
9377
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,...
0
9989
jinu1996
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...
1
9925
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,...
0
9811
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7358
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 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...
0
6640
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();...
0
5266
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...
1
3913
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.