473,398 Members | 2,212 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,398 software developers and data experts.

Operator overloading vs. class derivation/inheritance

Hello,

recently I wrote a little class which has to wrap two different type of
data, showing the same external interface.

I used operator overloading, but the same result I could eventually
obtain with two classes derived from an abstract base class containing
only the interface functions. So, when it's worth to use overloading
mechanism instead of abstract/inherited?
Jan 31 '06 #1
6 2822
Massimo Soricetti wrote:
Hello,

recently I wrote a little class which has to wrap two different type of
data, showing the same external interface.

I used operator overloading, but the same result I could eventually
obtain with two classes derived from an abstract base class containing
only the interface functions. So, when it's worth to use overloading
mechanism instead of abstract/inherited?


I think you'll have to define your example a little better.

Perhaps some code?

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Jan 31 '06 #2
Ben Pope ha scritto:
I think you'll have to define your example a little better.

Perhaps some code?


Sure, here it is (TipoCodeRef and TipoDato are "typedef enum"s):

<code>
#define XREF_CODICE true
#define XREF_DATA false

class Xref
{
private:
int TipoFonte;
static bool TipoReference; //true: code; false: data
public:
int Segmento;
unsigned int Offset;

inline Xref(TipoCodeRef F, int S, unsigned int O):
TipoReference(XREF_CODICE),
{
TipoFonte = int(F);
Segmento = S;
Offset = O;
};

inline Xref(TipoDato F, int S, unsigned int O): TipoReference(XREF_DATI),
{
TipoFonte = int(F);
Segmento = S;
Offset = O;
};

Xref(TipoCodeRef F, -1, unsigned int O);
Xref(TipoDato F, -1, unsigned int O);

// non negativa se č un reference a codice
inline TipoCodeRef GetRefType (void)
{
TipoReference ?
return TipoCodeRef(TipoFonte) :
return -1;
};

// non negativa se č un reference a dati
inline TipoDato GetRefType (void)
{
TipoReference ?
return -1 :
return TipoDato(TipoFonte);
};

inline bool IsCodeRef (void) {return TipoReference;};
};

</code>
Jan 31 '06 #3
In article <43**********************@reader3.news.tin.it>,
Massimo Soricetti <ma**********@hotmail.com> wrote:
Hello,

recently I wrote a little class which has to wrap two different type of
data, showing the same external interface.

I used operator overloading, but the same result I could eventually
obtain with two classes derived from an abstract base class containing
only the interface functions. So, when it's worth to use overloading
mechanism instead of abstract/inherited?


After looking over your code, I can't see where you did any operator
overloading, but anyway...

With the class you wrote, it's obvious that clients need to know which
type of data is contained in the object and be ready to handle either
type. This would be considered a bad idea in OO (ie using inheritance is
frowned upon in this type of situation.)

I can't help but wonder why you are combining the two different
data-types in one class?

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 2 '06 #4
Daniel T. wrote:
After looking over your code, I can't see where you did any operator
overloading, but anyway...
Oops... you are right, it's function overloading... :-|

With the class you wrote, it's obvious that clients need to know which
type of data is contained in the object and be ready to handle either
type. This would be considered a bad idea in OO (ie using inheritance is
frowned upon in this type of situation.)


So it's better to create an abstract base class and derive two other
classes from it in this situation? Any client of this class will manage
only one type of reference, so it calls only the function returning the
type it needs.
Feb 2 '06 #5

Massimo Soricetti wrote:
Daniel T. wrote:
After looking over your code, I can't see where you did any operator
overloading, but anyway...


Oops... you are right, it's function overloading... :-|

With the class you wrote, it's obvious that clients need to know which
type of data is contained in the object and be ready to handle either
type. This would be considered a bad idea in OO (ie using inheritance is
frowned upon in this type of situation.)


So it's better to create an abstract base class and derive two other
classes from it in this situation? Any client of this class will manage
only one type of reference, so it calls only the function returning the
type it needs.


It all depends on your needs. There is no one answer to your question.

That said, I work daily on code that used overloading instead of
polymorphism in a "dispatch" system. This means that yes, all clients
have to know what type they are working with and worst, you can never
use the generic type as the compiler won't know what to do with it; if
you have a pointer to the generic you have to establish what subclass
it is and cast it to a pointer of that type in order to call the
function. In this case there are some times when you need to do
something different with one type or the other (code breaks LSP
bigtime) but IMHO it should have been done in a function expecting the
generic type (it should have established if this was a special needs
type and put it where it wanted) or at the least only override in the
case when something different need be done; I expect someone though
maybe something different would be done down the road and so just
overrode for everything...you should see the switch statement during
the file read that does something like:

switch (readType)
case BLH_TYPE:
thingy->Dispatch(new BlhType(data)); // parsing constructor
break;
case BLF_TYPE:
thingy->Dispatch(new BlfType(data));
break;
....

Where I come from we call that fugly and it has a very bad odor (of
course the whole overloading thing is just one smell in this case, but
it is a bad one).

Feb 2 '06 #6
In article <43***********************@reader1.news.tin.it>,
Massimo Soricetti <ma**********@hotmail.com> wrote:
Daniel T. wrote:
After looking over your code, I can't see where you did any operator
overloading, but anyway...


Oops... you are right, it's function overloading... :-|

With the class you wrote, it's obvious that clients need to know which
type of data is contained in the object and be ready to handle either
type. This would be considered a bad idea in OO (ie using inheritance is
frowned upon in this type of situation.)


So it's better to create an abstract base class and derive two other
classes from it in this situation? Any client of this class will manage
only one type of reference, so it calls only the function returning the
type it needs.


No, it would be better to have two different classes, because clients
need to know what types they have.

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 2 '06 #7

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

Similar topics

16
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
5
by: James Angi | last post by:
I have a question on operator overloading that I can't find an answer to in several guides (even google has failed me). I'm currently making my way through several C++ guides, trying to become...
19
by: jacob navia | last post by:
C++ introduced an interesting feature (among others): operator overloading. The idea is to build a mechanism for the user defining its own number types and the operations to be done with them. ...
2
by: Tom Smith | last post by:
I'm having difficulty with overloading ==, and it's making my brain melt - can you help...? What I want to have is: 1) A base class A with virtual operator== defined (no problem) 2) A class B...
0
by: erik.erikson | last post by:
I am getting a compiler error that I can't well explain or even understand the origin of (though I boiled it down close...). Below is a bare-bones example. What I am doing is defining the...
5
by: toton | last post by:
Hi, I want a few of my class to overload from a base class, where the base class contains common functionality. This is to avoid repetition of code, and may be reducing amount of code in binary,...
11
by: dascandy | last post by:
Hello, I was wondering, why is overloading operator. (period) forbidden? It would make a few odd applications possible (dynamic inheritance and transparent remote method invocation spring to my...
3
by: Ravi | last post by:
Is this the correct way to think of "base class"? The "base class" is a class from which other classes are derived. The "base class" will never be derived from another class.
2
by: Peng Yu | last post by:
Hi, In the following code, the 'copy' member function works. But the '=' operator does not work. Can somebody let me know why a member function is different from an operator. Thanks, Peng ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.