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

How To overload =

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
Nov 16 '05 #1
6 4909
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" <hj*****@ix.netcom.com> schrieb im Newsbeitrag
news:A6*****************@newsread2.news.pas.earthl ink.net...
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

Nov 16 '05 #2
Yeah, what he said. :)

I thought I needed to overload = to create my own string-like class until I
read this article where I learned overloading the type conversion did the
trick.

http://www.c-sharpcorner.com/1/SuperStringMG.asp

"cody" <pl*************************@gmx.de> wrote in message
news:ei****************@TK2MSFTNGP11.phx.gbl...
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?

Nov 16 '05 #3
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" <pl*************************@gmx.de> wrote in message news:ei****************@TK2MSFTNGP11.phx.gbl...
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" <hj*****@ix.netcom.com> schrieb im Newsbeitrag
news:A6*****************@newsread2.news.pas.earthl ink.net...
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



Nov 16 '05 #4
Harry J. Smith <hj*****@ix.netcom.com> wrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Harry J. Smith <hj*****@ix.netcom.com> wrote:
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?"

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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #6
Stu Smith <st*****@nospam-digita.com> wrote:
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?"


Yup. Oops.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7

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

Similar topics

7
by: Piotre Ugrumov | last post by:
I have tried to implement the overload of these 2 operators. ostream & operator<<(ostream &out, Person &p){ out<<p.getName()<<" "<<p.getSurname()<<", "<<p.getDateOfBirth()<<endl; return out; }...
1
by: Piotre Ugrumov | last post by:
I'm following your help. I have written the overload of the operator <<. This overload work! :-) But I have some problem with the overload of the operator >>. I have written the overload of this...
3
by: Piotre Ugrumov | last post by:
I have done the overload on the operator >> and << in the class Attore. These 2 overload work correctly. I have done the overload of the same overload in the class Film. The class film ha inside...
4
by: Chiller | last post by:
Ok, thanks to some good assistance/advice from people in this group I've been able to further develop my Distance class. Since previous posts I've refined my code to accept the unit measurement...
9
by: Mario Charest | last post by:
.. Hi, It's possible to overload the operator, but I wonder if it's possible to somehow overload and so forth. My goal would be to switch from static array declaration into something...
17
by: Chris | last post by:
To me, this seems rather redundant. The compiler requires that if you overload the == operator, you must also overload the != operator. All I do for the != operator is something like this: ...
10
by: shachar | last post by:
hi all. can i OverLoad Operators - such as Exclamation Mark "!" ? how?
9
by: Tony | last post by:
I have an operator== overload that compares two items and returns a new class as the result of the comparison (instead of the normal bool) I then get an ambiguous operater compile error when I...
3
by: i3x171um | last post by:
To start off, I'm using GCC4. Specifically, the MingW (setjmp/longjmp) build of GCC 4.2.1 on Windows XP x64. I'm writing a class that abstracts a message, which can be either an integer (stored as...
5
by: jknupp | last post by:
In the following program, if the call to bar does not specify the type as <int>, gcc gives the error "no matching function for call to ‘bar(A&, <unresolved overloaded function type>)’". Since bar...
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?
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.