473,769 Members | 3,820 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

override ++ operater and Compiler problem when casting?

I have an abstract class, RuleResponse, and then create a new class base on
it, RuleResponseSeq uence, and override the ++ operater.

If I try to cast a RuleResponse as a RuleResponseSeq uence 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.Respons e is a RuleResponse
instance):
((RuleResponseS equence)m_rule. Response)++;

But if I write the following code, it compiles file:
RuleResponseSeq uence a = (RuleResponseSe quence)m_rule.R esponse; a++;

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

Tks.



Nov 13 '05 #1
1 2211
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
"Sacha Faust" <sf****@spidyna mics.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
I have an abstract class, RuleResponse, and then create a new class base on it, RuleResponseSeq uence, and override the ++ operater.

If I try to cast a RuleResponse as a RuleResponseSeq uence 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.Respons e is a RuleResponse
instance):
((RuleResponseS equence)m_rule. Response)++;

But if I write the following code, it compiles file:
RuleResponseSeq uence a = (RuleResponseSe quence)m_rule.R esponse; a++;

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

Tks.


Nov 13 '05 #2

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

Similar topics

5
11229
by: dis | last post by:
I've been going through my code and clearing away some of the compiler warnings that i'm generating and i've come across areas where i cast pointers to integer values. The Visual Studio compiler generates this warning ... "warning C4311: 'type cast' : pointer truncation from 'void *' to 'DWORD'" The warning is generated as the pointer is apparently 64 bits long (even though a quick sizeof() reports it to only be 4 bytes in length) and...
29
2526
by: junky_fellow | last post by:
Consider the following piece of code: struct junk { int i_val; int i_val1; char c_val; }; int main(void) {
14
12146
by: JPRoot | last post by:
Hi I use the following syntax to have events inherited from base to child classes which works nicely (virtual and override keyword on events). But I am wondering if it is a "supported" way of using events since I never saw it used anywhere in MSDN documentation/samples?! Or it will just break when I upgrade to .NET Framework 2.x in the coming years namespace MyNamespac public delegate void MyDel() public class MyBase public virtual...
5
2614
by: Mark Broadbent | last post by:
Oh yes its that chestnut again! Ive gone over the following (http://www.yoda.arachsys.com/csharp/faq/ -thanks Jon!) again regarding this subject and performed a few of my own tests. I have two classes yClass which inherits xClass. xClass has a virtual method which simply writes a line of text stating its origin, yClass implements the same method which writes a line of text stating its origin also (i.e. "From yClass"). I ran the...
15
3787
by: Cliff_Harker | last post by:
Why can't I do this in C# public class A { public A virtual whatever( A a ) { } } public class B : A
16
1745
by: ssg31415926 | last post by:
Could someone explain the implications of the differences between using 'new' and 'override' on a member declaration, please. I checked out MSDN but it's not exactly written for the beginner. E.g. on Name Hiding, it starts with this: "The scope of an entity typically encompasses more program text than the declaration space of the entity." That whizzing sound you're hearing is it going over my head! SSG
21
2084
by: bonk | last post by:
Did anyone EVER come across the need to use "new" in a method declaration (breaking with inheritance / versioning)? Allthough I know what it does and how it is used I really have a hard time to make up a usefull real life scenario for it. Maybe you can give a practical example from a real life?
5
4535
by: Marcel Hug | last post by:
Hi NG ! I'm new in C# and I'm reading a book about the fundamentals and concepts. In the chapter Methods it's written to use virtual, if i would like to override the method in a subclass. This I've to do by using override. It's also written, that's possible to "hide" the base class method by using the new key word. Because I've already written some C# code and I didn't know anything
5
2392
by: taumuon | last post by:
I've got an object, Person, that supports IEquatable<Person>. It implements bool Equals(Person obj) as well as overriding bool Equals(object obj) I've got a container type that holds a member object of generic type T, that supports IEquatable<T>, and a method, DoComparisons(T obj) to compare the member object to the object passed in.
0
9579
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
9420
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10035
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8863
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
7401
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
5293
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
3949
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
3556
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2811
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.