How to override the = operator?
-
I made a class for Complex Numbers. I don't know what to write in the body of my override function.
public ComplexNumber(int real, int imagine)
{
//Real and Imagine are attributes.
Real = real;
Imagine = imagine;
}public static ComplexNumber operator =(ComplexNumber a, ComplexNumber b)
{
return new /* ?????????? */;
}Are you trying to assign a to b here?
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
-
Are you trying to assign a to b here?
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
Yes, I want to do that but I've got that there is not such a possibility for the "=" operator, according to what Dave was trying to tell me.
-
Yes, I want to do that but I've got that there is not such a possibility for the "=" operator, according to what Dave was trying to tell me.
I can't see much sense in trying to assign a ComplexNumber from two others like this. Do you really mean it to be the summation of the two complex numbers to form the new one? If so, you would override the + instead. Have a look here[^] for details.
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
-
I can't see much sense in trying to assign a ComplexNumber from two others like this. Do you really mean it to be the summation of the two complex numbers to form the new one? If so, you would override the + instead. Have a look here[^] for details.
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
No as I said before, I somehow misunderstood the whole thing. By the way thanks for your attention and guidance :)
-
I made a class for Complex Numbers. I don't know what to write in the body of my override function.
public ComplexNumber(int real, int imagine)
{
//Real and Imagine are attributes.
Real = real;
Imagine = imagine;
}public static ComplexNumber operator =(ComplexNumber a, ComplexNumber b)
{
return new /* ?????????? */;
}You should typically implement a function
Clone
that will create a copy of your class. See MSDN documentation forIClonable
.public ComplexNumber Clone()
{
return new ComplexNumber { Real = this.Real, Imagine = this.Imagine };
}By the way if you want to implement
IClonable
, it would be preferable to use explicit interface implementation so that your regular Clone function could returns aComplexNumber
(not sure if it still necessary with recent C# compiler).Philippe Mori
-
I made a class for Complex Numbers. I don't know what to write in the body of my override function.
public ComplexNumber(int real, int imagine)
{
//Real and Imagine are attributes.
Real = real;
Imagine = imagine;
}public static ComplexNumber operator =(ComplexNumber a, ComplexNumber b)
{
return new /* ?????????? */;
}I guess you are a C++ programmer (your user name further reinforces that impression). In C#, you cannot override assignment;
a = b
for a reference type will always assign a to exactly the same reference as b, and it's not possible to change this behaviour. (I think this is good because it means you know what a line is doing, whereas hiding copying in = requires you to know things about the class you're using.) If you want to create copies of an object, you should implement ICloneable (but read this[^]), a copy method (e.g.public ComplexNumber Copy() { ... }
) and/or provide a copy constructor (i.e. a constructor that takes a ComplexNumber). -
I made a class for Complex Numbers. I don't know what to write in the body of my override function.
public ComplexNumber(int real, int imagine)
{
//Real and Imagine are attributes.
Real = real;
Imagine = imagine;
}public static ComplexNumber operator =(ComplexNumber a, ComplexNumber b)
{
return new /* ?????????? */;
}atoi_powered wrote:
I made a class for Complex Numbers
IMO you got a wrong starting point, special number systems (such as complex) is something I would implement as a value type (i.e. a struct), not a class. That would make your "instances" immutable, and it would remove the problem of assignment. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
atoi_powered wrote:
I made a class for Complex Numbers
IMO you got a wrong starting point, special number systems (such as complex) is something I would implement as a value type (i.e. a struct), not a class. That would make your "instances" immutable, and it would remove the problem of assignment. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
That's right! Thanks :)
-
I guess you are a C++ programmer (your user name further reinforces that impression). In C#, you cannot override assignment;
a = b
for a reference type will always assign a to exactly the same reference as b, and it's not possible to change this behaviour. (I think this is good because it means you know what a line is doing, whereas hiding copying in = requires you to know things about the class you're using.) If you want to create copies of an object, you should implement ICloneable (but read this[^]), a copy method (e.g.public ComplexNumber Copy() { ... }
) and/or provide a copy constructor (i.e. a constructor that takes a ComplexNumber).You're completely right. I should try that instead. And by the way, I know there is a function in C++ called atoi but It has another meaning to me. It actually represents my slogan in a short form which is the same with that function's name! :-D Thanks for your help :)
-
That's right! Thanks :)
you're welcome. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum