473,698 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1339
t f <th**********@g mail.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.co m>
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**********@g mail.comwrote in message
news:e6******** ******@TK2MSFTN GP02.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!=(Clas sA 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.co mwrote in message
news:MP******** *************@m snews.microsoft .com...
>t f <th**********@g mail.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.nowrot e:

<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
1522
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: // Assignment operators. virtual base<T>& operator=(const base<T>& rhs) = 0;
2
3086
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 &other)=0; } but in the sub class
11
2495
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 provide an implemation of the operator +. Now I want to write another generic class Plan<DType>, which can
7
4468
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 to be checking that the correct data type is used DataAcess sets an abstract class and methods
0
2676
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 looked for one on the net (I did browse the boost library though). It doesn't matter right now, anyway. To put it simply, Abstract Iterator is mainly a wrapper class. It helps
2
1893
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 built-in integer types, then I get 1000 permutations of the above statement. If I then test a dozen different operators, I get over 10,000 test operations.
3
5090
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 (Java). It stores character strings, and resizes itself to accommodate new strings when needed. Interface: ----------
8
3749
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 <(X a, X b); } Why is that? -- apart from the obvious reason that the specification doesn't
4
2062
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 class is created directly. If this is the case why don't we hide the constructor of abstract class into protected region? So that only object which inherits it can call the ctor of abstract class. In this way no one can create the object of...
0
8683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8901
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7739
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.