How To overload =

  • Thread starter Thread starter Harry J. Smith
  • Start date Start date
H

Harry J. Smith

How do you overload the = operator?

public static MultiUI operator = (MultiUI left, MultiUI right)

{

return right;

}

gives an error of "overloadable binary operator expected"



-Harry
 
You cannot overload operator=, but you can overload the type conversion
operator.
According to your example what sense would it make to overload operator=
which only assign one objects of type T to another object with exact the
same type?
 
When you do a = b, where a and b are two objects, only the pointer to b is copied so a and b are the same object. The disassembly
shows:

a = b;

000000d7 mov edi,dword ptr [ebp-18h]

I wanted a function to copy all of the fields of b to a and have two objects whose fields are currently equal. I can do this with a
function like SetTo(b, a), but I wanted to do it with the = operator. This may be a bad idea because if you could do it you would
loose the copy pointer method.

-Harry

cody said:
You cannot overload operator=, but you can overload the type conversion
operator.
According to your example what sense would it make to overload operator=
which only assign one objects of type T to another object with exact the
same type?

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Harry J. Smith said:
How do you overload the = operator?

public static MultiUI operator = (MultiUI left, MultiUI right)

{

return right;

}

gives an error of "overloadable binary operator expected"



-Harry
 
Harry J. Smith said:
When you do a = b, where a and b are two objects, only the pointer to
b is copied so a and b are the same object. The disassembly
shows:

a = b;

000000d7 mov edi,dword ptr [ebp-18h]

I wanted a function to copy all of the fields of b to a and have two
objects whose fields are currently equal. I can do this with a
function like SetTo(b, a), but I wanted to do it with the = operator.
This may be a bad idea because if you could do it you would
loose the copy pointer method.

So you want value type semantics for assignment. Do you need reference
type semantics elsewhere, or could you just make your type a reference
type?

Don't forget that assignments effectively happen elsewhere - what would
you want to happen when someone passed a parameter? Should that make a
new object or not?
 
Jon Skeet said:
Harry J. Smith said:
When you do a = b, where a and b are two objects, only the pointer to
b is copied so a and b are the same object. The disassembly
shows:

a = b;

000000d7 mov edi,dword ptr [ebp-18h]

I wanted a function to copy all of the fields of b to a and have two
objects whose fields are currently equal. I can do this with a
function like SetTo(b, a), but I wanted to do it with the = operator.
This may be a bad idea because if you could do it you would
loose the copy pointer method.

So you want value type semantics for assignment. Do you need reference
type semantics elsewhere, or could you just make your type a reference
type?

Didn't you mean to say "...or could you just make your type a _value_ type?"
 
Back
Top