p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Go Back   p2p.wrox.com Forums > C# and C > C# 2008 > C# 2008 aka C# 3.0
I forgot my password Register Now
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 28th, 2009, 02:04 AM
Registered User
Points: 27, Level: 1
Points: 27, Level: 1 Points: 27, Level: 1 Points: 27, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2008
Location: , , .
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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old January 28th, 2009, 08:01 AM
Friend of Wrox
Points: 6,570, Level: 34
Points: 6,570, Level: 34 Points: 6,570, Level: 34 Points: 6,570, Level: 34
Activity: 42%
Activity: 42% Activity: 42% Activity: 42%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,000
Thanks: 5
Thanked 37 Times in 36 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.
================================================== =========
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #3 (permalink)  
Old January 28th, 2009, 09:25 AM
Registered User
Points: 27, Level: 1
Points: 27, Level: 1 Points: 27, Level: 1 Points: 27, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2008
Location: , , .
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..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #4 (permalink)  
Old January 28th, 2009, 06:30 PM
Friend of Wrox
Points: 4,805, Level: 29
Points: 4,805, Level: 29 Points: 4,805, Level: 29 Points: 4,805, Level: 29
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,323
Thanks: 3
Thanked 70 Times in 69 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#???
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #5 (permalink)  
Old February 5th, 2009, 12:24 AM
Registered User
Points: 27, Level: 1
Points: 27, Level: 1 Points: 27, Level: 1 Points: 27, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2008
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes it should be static in C#.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #6 (permalink)  
Old March 13th, 2009, 05:33 PM
Authorized User
Points: 240, Level: 4
Points: 240, Level: 4 Points: 240, Level: 4 Points: 240, Level: 4
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2008
Location: , , .
Posts: 68
Thanks: 0
Thanked 0 Times in 0 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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #7 (permalink)  
Old October 31st, 2009, 08:09 AM
Authorized User
Points: 125, Level: 2
Points: 125, Level: 2 Points: 125, Level: 2 Points: 125, Level: 2
Activity: 12%
Activity: 12% Activity: 12% Activity: 12%
 
Join Date: Oct 2004
Location: Fayetteville, AR, USA.
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 08:27 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

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



All times are GMT -4. The time now is 08:01 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc