Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2008 > C# 2008 aka C# 3.0
|
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 Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 28th, 2009, 02:04 AM
Registered User
 
Join Date: Feb 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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?
 
Old January 28th, 2009, 08:01 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

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.
================================================== =========
 
Old January 28th, 2009, 09:25 AM
Registered User
 
Join Date: Feb 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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..
 
Old January 28th, 2009, 06:30 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

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#???
 
Old February 5th, 2009, 12:24 AM
Registered User
 
Join Date: Feb 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes it should be static in C#.
 
Old March 13th, 2009, 04:33 PM
Friend of Wrox
 
Join Date: Dec 2008
Posts: 238
Thanks: 2
Thanked 20 Times in 19 Posts
Default

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.
 
Old October 31st, 2009, 07:09 AM
Authorized User
 
Join Date: Oct 2004
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to C@uark
Default

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 07:27 AM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
Invalid operator for data type. Operator equals di Pusstiu SQL Server 2000 2 August 10th, 2007 04:51 AM
Overloading nayeem69 .NET Framework 2.0 1 July 17th, 2007 12:30 AM
Operator Overloading In Inheritance ahmedsalam C# 0 October 18th, 2005 11:25 AM
operator overloading in VB.NET. rupen General .NET 4 April 21st, 2005 10:15 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.