inheritance in value types of = operator overloading in reference types

  • Thread starter Thread starter apm
  • Start date Start date
A

apm

Recently I have had to use a value type for a complex structure because I
don't know how to override the = operator. Can the operator ever be
overloaded? Or can inheritance be used with value types?
 
apm,

Inheritance and the = operator being overloaded are two different
things. First, you can not inherit from value types.

Second, you can not overload the = operator.

Why did you think that using a value type would allow you to overload
the = operator?

You might be able to get away with what you are doing by overloading the
casting (implicit and explicit).

Hope this helps.
 
Nicholas Paldino said:
apm,

Inheritance and the = operator being overloaded are two different
things. First, you can not inherit from value types.

Second, you can not overload the = operator.

Why did you think that using a value type would allow you to overload
the = operator?

The = does what I want it to do in a value type. I don't have to overload
it. I just wanted to 'have my cake and eat it too'. Why won't inheritance
work with value types?
You might be able to get away with what you are doing by overloading
the casting (implicit and explicit).

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

apm said:
Recently I have had to use a value type for a complex structure because I
don't know how to override the = operator. Can the operator ever be
overloaded? Or can inheritance be used with value types?
 
apm,

Structures (value types) can not be inherited from. That's just the way
it is.

So you want copy semantics on assignment. You should just implement the
IClonable interface and have it return the results of MemberwiseClone, as
this will do the same thing that assigning a structure to a variable of that
structure's type does.

Of course, you should do this on a reference type. Doing it on a
structure is a bit redundant.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

apm said:
Nicholas Paldino said:
apm,

Inheritance and the = operator being overloaded are two different
things. First, you can not inherit from value types.

Second, you can not overload the = operator.

Why did you think that using a value type would allow you to overload
the = operator?

The = does what I want it to do in a value type. I don't have to overload
it. I just wanted to 'have my cake and eat it too'. Why won't inheritance
work with value types?
You might be able to get away with what you are doing by overloading
the casting (implicit and explicit).

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

apm said:
Recently I have had to use a value type for a complex structure because
I don't know how to override the = operator. Can the operator ever be
overloaded? Or can inheritance be used with value types?
 
Nicholas Paldino said:
apm,

Structures (value types) can not be inherited from. That's just the
way it is.

So you want copy semantics on assignment. You should just implement
the IClonable interface and have it return the results of MemberwiseClone,
as this will do the same thing that assigning a structure to a variable of
that structure's type does.

Thank you. Forgot I could do that. I'll assume that is the same as
overloding Clone().

Of course, you should do this on a reference type. Doing it on a
structure is a bit redundant.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

apm said:
Nicholas Paldino said:
apm,

Inheritance and the = operator being overloaded are two different
things. First, you can not inherit from value types.

Second, you can not overload the = operator.

Why did you think that using a value type would allow you to overload
the = operator?

The = does what I want it to do in a value type. I don't have to overload
it. I just wanted to 'have my cake and eat it too'. Why won't
inheritance work with value types?
You might be able to get away with what you are doing by overloading
the casting (implicit and explicit).

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Recently I have had to use a value type for a complex structure because
I don't know how to override the = operator. Can the operator ever be
overloaded? Or can inheritance be used with value types?
 
apm,

You can't overload Clone, since object doesn't expose it by default.
You have to implement it.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

apm said:
Nicholas Paldino said:
apm,

Structures (value types) can not be inherited from. That's just the
way it is.

So you want copy semantics on assignment. You should just implement
the IClonable interface and have it return the results of
MemberwiseClone, as this will do the same thing that assigning a
structure to a variable of that structure's type does.

Thank you. Forgot I could do that. I'll assume that is the same as
overloding Clone().

Of course, you should do this on a reference type. Doing it on a
structure is a bit redundant.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

apm said:
in message apm,

Inheritance and the = operator being overloaded are two different
things. First, you can not inherit from value types.

Second, you can not overload the = operator.

Why did you think that using a value type would allow you to
overload the = operator?

The = does what I want it to do in a value type. I don't have to
overload it. I just wanted to 'have my cake and eat it too'. Why won't
inheritance work with value types?


You might be able to get away with what you are doing by overloading
the casting (implicit and explicit).

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Recently I have had to use a value type for a complex structure
because I don't know how to override the = operator. Can the operator
ever be overloaded? Or can inheritance be used with value types?
 
Back
Top