|
|
 |
| C# 2008 aka C# 3.0 Discuss the Visual C# 2008 (aka C# 3.0) language |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the C# 2008 aka C# 3.0 section of the Wrox p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.
|
 |

January 28th, 2009, 02:04 AM
|
|
Registered User
|
|
Join Date: Feb 2008
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Operator overloading issue
Hi ,
I made a small program in which I overloaded an operator in base class
and few other operators in derived class , now while using all of them from
main using derive class object it's giving problem as it says that:
can't implicitly convert base class to derive one.
Why I can't use base class overloaded operator with derived one? What is the work around of it?
|

January 28th, 2009, 08:01 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,000
Thanks: 5
Thanked 37 Times in 36 Posts
|
|
Hello. Can you provide some examples of what are you doing??
__________________
HTH
Gonzalo
================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the proof.
================================================== =========
|

January 28th, 2009, 09:25 AM
|
|
Registered User
|
|
Join Date: Feb 2008
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Ok here is the sample example:
Code:
public class Base
{
private int a;
public Base(int a)
{
this.a = a;
}
public static Base operator ++(Base B)
{
return new Base(B.a + 1);
}
}
class Derive : Base
{
private int d;
public Derive(int d, int a)
: base(a)
{
this.d = d;
}
}
now in Main, I was trying this:
Code:
Derive d = new Derive(1, 2);
Base b = ++d; // here the error occurs.
I thought on it and found that it is not possible. As compiler understand that it has to call the base class overloaded operator but the parameter is actually a derived one even it inherits the Base class.
There is a naive approach to achieve it as:
Code:
Derive d = new Derive(1, 2);
Base a = d;
Base b = ++a; // no error now.
Since you asked to give a sample example of it, so this is it..
Thanks..
|

January 28th, 2009, 06:30 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,323
Thanks: 3
Thanked 70 Times in 69 Posts
|
|
Interesting discovery. I think it's because your operator is static.
I remember doing this years ago in C++, but I sure don't remember using static operators. Is this a requirement of C#???
|

February 5th, 2009, 12:24 AM
|
|
Registered User
|
|
Join Date: Feb 2008
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Yes it should be static in C#.
|

March 13th, 2009, 05:33 PM
|
|
Authorized User
|
|
Join Date: Dec 2008
Location: , , .
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I have no problem with the error you got, as the compiler simply has an issue with implicit cast. But strangly, with explicit casting, the compiler got further confused:
Code:
Base b = ++ (d as Base);
Base bb = ++ ((Base)d);
None of the above passes compiler and it complains that the ++ operator is not followed by a variable, when it certainly does. I think the parser somehow got confused. I would view this as a bug in the tool.
|

October 31st, 2009, 08:09 AM
|
|
Authorized User
|
|
Join Date: Oct 2004
Location: Fayetteville, AR, USA.
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
its becuse the compiler does not implictly cast when a loss of data could occur. Since going from Derive to Base (a down cast) could poteintial lead to a loss of data without examining what exactly is going on(which complier does not do), the compiler issue an error. This is a "strongly typed language" feature to weed out bugs, there is nothing wrong with your code in terms of OOP; the compiler just wants you explicitly do the cast to insure thats what you inteneded.
The next error in question:
"None of the above passes compiler and it complains that the ++ operator is not followed by a variable, when it certainly does."
is not really true, looks like it is but its not. need to understand interpreters and evaluators for this one. when the compiler builds its parse tree. it will do something like this(this is not exactly what happens but the theory behind it)
.......................(++) (requires an L value, a variable to store result in)
..........................|
....................(Base) (type cast operation)
.............|...................|
........new type ......old type
...........Base ........Derived d (is evaluated before being type cast to new type)
When the type cast operation is evaluated the variable name is evaluated(value extracted from variable), converted to the new type and returned as a value(with type Base) to the increment operator where it is expecting an L(left hand side) value a.k.a, a vaiable to store the result of the increment in. Since the varibable will be lost on the upward climb from type casting and all that would remain is the value, the increment can not store the result in a meaningful persistant location, so to prevent this from happening the systax checker flags this before running so not to produce a run time error .
__________________
Mike
Last edited by C@uark : October 31st, 2009 at 08:27 AM.
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |