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

How to override the operator =


Hi All :
How to override the class operator = like C++ ?

Best regards !

Nov 15 '05 #1
11 21465
I am sorry that you can't override operator= in C#.
"apex" <ap*****@sjtu.edu.cn> wrote in message
news:el**************@tk2msftngp13.phx.gbl...

Hi All :
How to override the class operator = like C++ ?

Best regards !

Nov 15 '05 #2
You can override the implicit cast - operator. (see keyword implicit in
MSDN)

"apex" <ap*****@sjtu.edu.cn> wrote in message
news:el**************@tk2msftngp13.phx.gbl...

Hi All :
How to override the class operator = like C++ ?

Best regards !

Nov 15 '05 #3
Could you adjust your clock?

José
"apex" <ap*****@sjtu.edu.cn> wrote in message
news:el**************@tk2msftngp13.phx.gbl...

Hi All :
How to override the class operator = like C++ ?

Best regards !

Nov 15 '05 #4
Simon Trew <ten.egnaro@werts> wrote:
If the poster is in China (.cn address) the clock may be correct.


Then their timezone isn't.

The original post had a date header of:

Date: Tue, 19 Aug 2003 11:15:18 -0700

That can't possibly be right, as that time (18:15 GMT) hasn't yet
happened.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #5
Code in C++ :-) or just override equals in C#
Ashish

"apex" <ap*****@sjtu.edu.cn> wrote in message
news:el**************@tk2msftngp13.phx.gbl...

Hi All :
How to override the class operator = like C++ ?

Best regards !

Nov 15 '05 #6
"Ashish" <ab*@hotmail.com> wrote in message
news:eR**************@tk2msftngp13.phx.gbl...
Code in C++ :-) or just override equals in C#
Ashish


Wouldn't that override "==", not "="?

Can I ask why you would possibly want to override "="? There must be another
solution.

Nathan
Nov 15 '05 #7
For example:
MyClass A = new MyClass();
MyClass B = new MyClass();
A.Item1 = ...;
A.Item2 = ...;
B = A;

"Treefrog" <tr******@mountainbikes.co.uk> wrote in message
news:3f********@news.1anetworks.net...
"Ashish" <ab*@hotmail.com> wrote in message
news:eR**************@tk2msftngp13.phx.gbl...
Code in C++ :-) or just override equals in C#
Ashish
Wouldn't that override "==", not "="?

Can I ask why you would possibly want to override "="? There must be

another solution.

Nathan

Nov 15 '05 #8
I apologize I mean override not overload....its been a long week :)

"Chris Blanco" <cb***********@necam.com> wrote in message
news:eC**************@TK2MSFTNGP12.phx.gbl...
I don't think you can override assignment operators. Look into overloading
the MemberwiseClone() function if you need capability beyond what it gives
you.

Using your example it would be:
MyClass A = new MyClass();
MyClass B = new MyClass();
....
B=(MyClass)A.MemberwiseClone();

"apex" <ap*****@sjtu.edu.cn> wrote in message
news:O%****************@TK2MSFTNGP10.phx.gbl...
For example:
MyClass A = new MyClass();
MyClass B = new MyClass();
A.Item1 = ...;
A.Item2 = ...;
B = A;

"Treefrog" <tr******@mountainbikes.co.uk> wrote in message
news:3f********@news.1anetworks.net...
"Ashish" <ab*@hotmail.com> wrote in message
news:eR**************@tk2msftngp13.phx.gbl...
> Code in C++ :-) or just override equals in C#
> Ashish

Wouldn't that override "==", not "="?

Can I ask why you would possibly want to override "="? There must be

another
solution.

Nathan



Nov 15 '05 #9
You cannot override/overload or otherwise modify
the assignment (=) or new operators in .NET.

There is specific logic that .NET uses for assignment
that is required for the memory management and
garbage collection.

I think what you may want is to override the
implicit and explicit cast operator.

For example:

MyFancyNumberClass mfnc = new MyFancyNumberClass():

// implicit cast
int foo = mfnc;

If you wrote MyFancyNumberClass and you wanted
people to copy it to an int, you'd use the
implicit cast operator to convert mfnc to an
integer.

You can also override explicit casts:
int foo = (int) mfnc;

-c

"apex" <ap*****@sjtu.edu.cn> wrote in message
news:O%****************@TK2MSFTNGP10.phx.gbl...
For example:
MyClass A = new MyClass();
MyClass B = new MyClass();
A.Item1 = ...;
A.Item2 = ...;
B = A;

"Treefrog" <tr******@mountainbikes.co.uk> wrote in message
news:3f********@news.1anetworks.net...
"Ashish" <ab*@hotmail.com> wrote in message
news:eR**************@tk2msftngp13.phx.gbl...
Code in C++ :-) or just override equals in C#
Ashish


Wouldn't that override "==", not "="?

Can I ask why you would possibly want to override "="? There must be

another
solution.

Nathan


Nov 15 '05 #10
OK ,Thanks a lot .
"Chad Myers" <cm****@N0.SP.4M.austin.rr.com> wrote in message
news:O2*******************@twister.austin.rr.com.. .
You cannot override/overload or otherwise modify
the assignment (=) or new operators in .NET.

There is specific logic that .NET uses for assignment
that is required for the memory management and
garbage collection.

I think what you may want is to override the
implicit and explicit cast operator.

For example:

MyFancyNumberClass mfnc = new MyFancyNumberClass():

// implicit cast
int foo = mfnc;

If you wrote MyFancyNumberClass and you wanted
people to copy it to an int, you'd use the
implicit cast operator to convert mfnc to an
integer.

You can also override explicit casts:
int foo = (int) mfnc;

-c

"apex" <ap*****@sjtu.edu.cn> wrote in message
news:O%****************@TK2MSFTNGP10.phx.gbl...
For example:
MyClass A = new MyClass();
MyClass B = new MyClass();
A.Item1 = ...;
A.Item2 = ...;
B = A;

"Treefrog" <tr******@mountainbikes.co.uk> wrote in message
news:3f********@news.1anetworks.net...
"Ashish" <ab*@hotmail.com> wrote in message
news:eR**************@tk2msftngp13.phx.gbl...
> Code in C++ :-) or just override equals in C#
> Ashish

Wouldn't that override "==", not "="?

Can I ask why you would possibly want to override "="? There must be

another
solution.

Nathan



Nov 15 '05 #11
That my my answer 2 days before ;-)

GP

"apex" <ap*****@sjtu.edu.cn> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
OK ,Thanks a lot .
"Chad Myers" <cm****@N0.SP.4M.austin.rr.com> wrote in message
news:O2*******************@twister.austin.rr.com.. .
You cannot override/overload or otherwise modify
the assignment (=) or new operators in .NET.

There is specific logic that .NET uses for assignment
that is required for the memory management and
garbage collection.

I think what you may want is to override the
implicit and explicit cast operator.

For example:

MyFancyNumberClass mfnc = new MyFancyNumberClass():

// implicit cast
int foo = mfnc;

If you wrote MyFancyNumberClass and you wanted
people to copy it to an int, you'd use the
implicit cast operator to convert mfnc to an
integer.

You can also override explicit casts:
int foo = (int) mfnc;

-c

"apex" <ap*****@sjtu.edu.cn> wrote in message
news:O%****************@TK2MSFTNGP10.phx.gbl...
For example:
MyClass A = new MyClass();
MyClass B = new MyClass();
A.Item1 = ...;
A.Item2 = ...;
B = A;

"Treefrog" <tr******@mountainbikes.co.uk> wrote in message
news:3f********@news.1anetworks.net...
> "Ashish" <ab*@hotmail.com> wrote in message
> news:eR**************@tk2msftngp13.phx.gbl...
> > Code in C++ :-) or just override equals in C#
> > Ashish
>
> Wouldn't that override "==", not "="?
>
> Can I ask why you would possibly want to override "="? There must be
another
> solution.
>
> Nathan
>
>



Nov 15 '05 #12

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

Similar topics

3
by: Mathieu Malaterre | last post by:
Hello, I am trying to override the delete operator so that it set 0 to my object. Is this possible ? I am used to do: obj *a = new obj; .... delete a;
3
by: Matt | last post by:
Hello, I'm trying to implement the design below. I'd prefer to use commented operator+() in class Number instead of the associated, uncommented operator+(), but as I understand Covariant Return...
3
by: James Aguilar | last post by:
I am having a problem with a derived class not seeing an operator in a base class. Suppose I have a base class: template <typename T> class Array { protected: T *m_array;
1
by: Sacha Faust | last post by:
I have an abstract class, RuleResponse, and then create a new class base on it, RuleResponseSequence, and override the ++ operater. If I try to cast a RuleResponse as a RuleResponseSequence and...
8
by: JPRoot | last post by:
Hi M. Jeffrey Tan, Just hopping you didn't forget me? :) Thanks JPRoot ----- \"Jeffrey Tan\" wrote: -----
7
by: AWHK | last post by:
How can I force anyone who subclasses my class to override the ToString() method? Andreas :-)
2
by: Shark | last post by:
Hi, if we need to change the behavior of operator new, it is called overriding or overloading? My other question is, if we change the behavior of operator new, do we use malloc to do that or we use...
5
by: meng.frank | last post by:
If I want to use std::vector or std::list for my class MyNewClass, which operators should I override? Maybe operator new and operator delete? If I want to use MyNewClass in std::set or std::map,...
52
by: Jim Langston | last post by:
I wanted to do an operator override for but couldnt' figure out the syntax. I tried this (code that doesn't compile commented out with //: class CMyBitmap { public: CMyBitmap( int Rows, int...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
0
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...

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.