override ++ operater and Compiler problem when casting?

S

Sacha Faust

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):


((RuleResponseSequence)m_rule.Response)++;

But if I write the following code, it compiles file:
RuleResponseSequence a = (RuleResponseSequence)m_rule.Response; a++;



I would like to know if this is a "bug" or if C# handles cast differently.



Tks.
 
1

100

Hi Sacha,
There is two reasons for that:
Th first is the way overloaded operation ++ works and the second is the way
cast operation works ;))))
1. The overloaded ++ operation has to create a new instance of the class
with modified internal state and return this new istance, which will be
saved in the given location.
for example ++x will produce the following actions
- x's operator ++ will be called and a new object of the same class (let say
XClass) will be created. Let name it tempx
- and the x = tempx
the result of the operation will be either original x or tempx according of
whether postfix or prefix operation has been applied.

2. The cast operation works like that:
- places in the evaluation stack a address of (refrence to) the object in
the managed heap
- pops the reference and tries to cast the reference to the target type: If
it is predefined cast (your case) I believe it adjusts only the address, if
it is an overloaded cast operation - calls the method, which creates the
new object (new address)
- pushes the new address (refrence) into the evaluation stack.

Let what you want to do is legal:
((SomeClass)a)++;
So, cast operation will generate some address (different tha the address
stored in 'a'). this address will be used to call the overloaded ++
operation this operation will create a new object of type SomeClass (new
address) and this address has to be saved back to the variable, which holds
the address used to call the ++ operation. Opps, no such a variable. The
effect of the ++ operation cannot be saved anywhere. Got the picture?

There is only one case I can think of, where this operation can be performed
safely. And this is your case when the cast is from the base class to
derived class which is the actual type of the object.:
The class hierarachy is like that:
BaseClass <-- DerivedClass
BaseClass a = new DerivedClass();
((DerivedClass)a)++;
Even if c# compiler generates code to store the newly created object back to
'a' there no problem because they are of the same actual (run-time) type.
Check out the following , though:

DerivedClass a = new DerivedClass()
((BaseClass)a)++;
The result will be object of class BaseClass, which has to be stored in
variable of DerivedClass. This is totally incorrect.
Think is getting even worst if we use overloaded type-cast operation where
the new type will be absolutely different than the type of the variable.

That's the reason this is not allowed. Even if the only safe case is
possible to be determined at compile time, using it will be error prone.

HTH
B/rgds
100
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top