473,378 Members | 1,119 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

abstract class question about operators

t f
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
Jun 26 '07 #1
4 1329
t f <th**********@gmail.comwrote:

<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.

--
Jon Skeet - <sk***@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
Jun 26 '07 #2

"t f" <th**********@gmail.comwrote in message
news:e6**************@TK2MSFTNGP02.phx.gbl...
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?
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

Jun 26 '07 #3
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
>t f <th**********@gmail.comwrote:
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
Jun 27 '07 #4
On Jun 27, 11:39 am, "Michael S" <n...@no.nowrote:

<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

Jun 27 '07 #5

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

Similar topics

7
by: Jef Driesen | last post by:
Suppose I have an abstract base class that looks like this: template <typename T> class base { public: // Typedefs typedef double value_type; typedef std::size_t size_type; public: //...
2
by: www.brook | last post by:
I want to enfore the implementation of the operator overloading eg class Interface_ { public: virtual void operator = (const Interface &other)=0; virtual void operator +=(const Interface...
11
by: herpers | last post by:
Hello, I probably don't see the obvious, but maybe you can help me out of this mess. The following is my problem: I created two classes NormDistribution and DiscDistribution. Both classes...
7
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears...
0
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never...
2
by: Dom Jackson | last post by:
Hello - I have a problem where I need to test some numeric code using a variety of built-in integer types: obj_type1 = obj_type2 OP obj_type3; // is obj_type1 correct? If I test with 10...
3
by: jacob navia | last post by:
Abstract: Continuing the discussion about abstract data types, in this discussion group, a string collection data type is presented, patterned after the collection in C# and similar languages...
8
by: Paul E Collins | last post by:
This code won't compile because "the modifier 'abstract' is not valid for this item". abstract class X { public abstract static bool operator >(X a, X b); public abstract static bool operator...
4
by: Pranav | last post by:
Hello All, I have a simple question regarding the definition of abstract class, IIRC , Abstract class is one which contains virtual function declaration and other variables and no object of this...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.