473,666 Members | 2,368 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to override the operator =


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

Best regards !

Nov 15 '05 #1
11 21488
I am sorry that you can't override operator= in C#.
"apex" <ap*****@sjtu.e du.cn> wrote in message
news:el******** ******@tk2msftn gp13.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.e du.cn> wrote in message
news:el******** ******@tk2msftn gp13.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.e du.cn> wrote in message
news:el******** ******@tk2msftn gp13.phx.gbl...

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

Best regards !

Nov 15 '05 #4
Simon Trew <ten.egnaro@wer ts> 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.co m>
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.e du.cn> wrote in message
news:el******** ******@tk2msftn gp13.phx.gbl...

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

Best regards !

Nov 15 '05 #6
"Ashish" <ab*@hotmail.co m> wrote in message
news:eR******** ******@tk2msftn gp13.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******@mount ainbikes.co.uk> wrote in message
news:3f******** @news.1anetwork s.net...
"Ashish" <ab*@hotmail.co m> wrote in message
news:eR******** ******@tk2msftn gp13.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******** ******@TK2MSFTN GP12.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.Me mberwiseClone() ;

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

"Treefrog" <tr******@mount ainbikes.co.uk> wrote in message
news:3f******** @news.1anetwork s.net...
"Ashish" <ab*@hotmail.co m> wrote in message
news:eR******** ******@tk2msftn gp13.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:

MyFancyNumberCl ass mfnc = new MyFancyNumberCl ass():

// implicit cast
int foo = mfnc;

If you wrote MyFancyNumberCl ass 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.e du.cn> wrote in message
news:O%******** ********@TK2MSF TNGP10.phx.gbl. ..
For example:
MyClass A = new MyClass();
MyClass B = new MyClass();
A.Item1 = ...;
A.Item2 = ...;
B = A;

"Treefrog" <tr******@mount ainbikes.co.uk> wrote in message
news:3f******** @news.1anetwork s.net...
"Ashish" <ab*@hotmail.co m> wrote in message
news:eR******** ******@tk2msftn gp13.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

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

Similar topics

3
1901
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
1687
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 Types, one is not allowed to return non-references (eg, references or pointers) in virtual-functions (which seems to make sense given the seemingly polymorphic nature). So...I use the hack below and make my Object-return function be
3
3642
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
2206
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 try to use the ++ operator the compiler tells me "The left-hand size of an assignment must be a variable, property or indexer". I don't understand why I get that. Here is the line that doesn't compile (m_rule.Response is a RuleResponse instance):
8
2887
by: JPRoot | last post by:
Hi M. Jeffrey Tan, Just hopping you didn't forget me? :) Thanks JPRoot ----- \"Jeffrey Tan\" wrote: -----
7
4549
by: AWHK | last post by:
How can I force anyone who subclasses my class to override the ToString() method? Andreas :-)
2
11518
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 operator new?
5
3442
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, which operators should I override in order to make the key unique? Thank you for your help,
52
3459
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 Columns ): Rows_( Rows ), Columns_( Columns ) { Data_ = new SPixel; }
0
8440
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8866
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8638
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7381
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6191
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4193
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2769
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2006
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.