Skip to main content
gamedev.net
🔒 Locked

Comparing objects "a==b"

Started by geo2004 4 replies 1.3k views
Original Post
geo2004
geo2004
Let's say I have a class called Thing, I instantiate 2 separate objects of this class. Thing a = new Thing(); Thing b = new Thing(); In C#, if I compare if(a==b){}, how will it do that comparison if I haven't overloaded that operator? Will it look at all the member variables of that class and compare them, or does it just compare the address location of each object? Doesn't C# store/pass most of the objects by reference? So would the code above in C++ be:? Thing *a = new Thing(); Thing *b = new Thing(); Is that correct? Or am I way off? Jeff
Cornstalks
Cornstalks
Quote:
From http://msdn.microsoft.com/en-us/library/53k8ybth(VS.71).aspx#
For reference types other than string, == returns true if its two operands refer to the same object.
If you haven't overloaded the == operator, it will compare the references (i.e. the address location) (at least for classes, I'm not sure if it will for structs). C# passes some things by value and some things by reference. Specifically, structs are passed by value, whereas classes are passed by reference. So yes, that is roughly the equivalent C++ code, since Thing is a class.
smitty1276
smitty1276
Quote:
Original post by MikeTacular
So yes, that is roughly the equivalent C++ code, since Thing is a class.
To clarify, it is roughly equivalent to comparing pointers in C++, not objects.

DevFred
DevFred
Quote:
Original post by MikeTacular
classes are passed by reference.

Last time I checked, class references were passed by value.
Ibasa
Ibasa
Class references are passed by value, but seeing as its a reference to the class a lot of people say that the class is passed by referance.

C# to C++ code roughly goes like this

C#:
SomeStruct a = new SomeStruct();
C++:
SomeStruct a();

C#:
SomeClass a = new SomeClass();
C++:
SomeClass* a = new SomeClass();

C#:
void Function(SomeStruct a, ref SomeStruct b, SomeClass c, ref SomeClass d);
C++:
void Function(SomeStruct a, SomeStruct& b, SomeClass* c, SomeClass&* d);
(The last one is a referance to a pointer, not totatlly sure thats the correct C++ code for it);
DevFred
DevFred
Quote:
Original post by Ibasa
(The last one is a referance to a pointer, not totatlly sure thats the correct C++ code for it);

I believe you have to switch the & and the *

void Function(SomeStruct a, SomeStruct& b, SomeClass* c, SomeClass*& d);

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.