473,387 Members | 1,364 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,387 software developers and data experts.

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 1421
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.ReferenceEquals 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.ReferenceEquals 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(lhs, 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.net
Jan 12 '06 #4

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

Similar topics

6
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...
3
by: matt p | last post by:
example: FunClass myfun; FunClass *lotsofunptr=&myfun; myfun; //calls the overloaded operator; lotsofunptr->;//error help is much apreciated
8
by: SpOiLeR | last post by:
Hello! I have a matrix class like this: class MyObject; // MyMatrix is contains MyObjects class MyMatrix { public: ...
4
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
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...
5
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....
13
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...
2
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...
1
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,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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 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.