Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / June 2007

Tip: Looking for answers? Try searching our database.

abstract class question about operators

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
t f - 26 Jun 2007 18:20 GMT
hi

just a quick question - i  have the something like the following

public abstract class ClassA
{
   protected int iA;

   public int Value { get {return iA;} set { iA = value;}}

   public ClassA(int value)
   { iA = value; }

    public static ClassA operator +(ClassA a1, ClassA a2)
    {
        a1.Value = a1.Value + a2.Value;
         return a1;
   }

    public static ClassA operator -(ClassA a1, ClassA a2)
    {
        a1.Value = a1.Value - a2.Value;
         return a1;
   }
}

public class ClassB : ClassA
{
     public ClassB(int value) : base(value)
     { }
}

public class ClassC : ClassA
{
     public ClassB(int value) : base(value)
     { }
}

Problem is, if i try this
ClassB cb1 = new ClassB(1);
ClassB cb2 = new ClassB(2);
ClassB cb3 = cb1 + cb2;

I get an error as cb1 + cb2 is of type ClassA....

Question: Is there a way of putting all my operator overloads in the base
class and instead of having to put them in each of the derived classes?

Thanks
t f
Jon Skeet [C# MVP] - 26 Jun 2007 18:54 GMT
<snip>

> Question: Is there a way of putting all my operator overloads in the base
> class and instead of having to put them in each of the derived classes?

No - the base class would have to know what to do with each of the
derived classes, which it shouldn't care about. (You also can't write
an operator which doesn't use your own type as one of the operands,
IIRC.)

You'll need to overload the operators in each derived class you want to
use them for.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Michael S - 27 Jun 2007 11:39 GMT
> No - the base class would have to know what to do with each of the
> derived classes, which it shouldn't care about. (You also can't write
> an operator which doesn't use your own type as one of the operands,
> IIRC.)

What Jon is saying here is that:

public static ClassA operator +(ClassA a1, ClassA a2)
{
        a1.Value = a1.Value + a2.Value;
         return a1;
}

should be:

public static ClassA operator +(ClassA a1, ClassA a2)
{
        return new ClassA(a1.Value + a2.Value);
}

- Michael S
Jon Skeet [C# MVP] - 27 Jun 2007 12:09 GMT
<snip>

> What Jon is saying here is that:

In fact, that's not quite what I was saying - I hadn't spotted that
the original code was changing an existing value rather than creating
a new one. I'd certainly have said that *as well* if I'd spotted it
though :)

Jon
Ben Voigt [C++ MVP] - 26 Jun 2007 19:02 GMT
> hi
>
[quoted text clipped - 43 lines]
> Question: Is there a way of putting all my operator overloads in the base
> class and instead of having to put them in each of the derived classes?

You've already done so.  You've also discovered the limitations, which have
to do with covariance.

This method could certainly work for
static bool operator!=(ClassA a1, ClassA a2);
and similar, but the return type must be fixed and doesn't vary with the
derived type.

> Thanks
> t f

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.