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

question about operator overload and abstract class

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
class A:public Interface_
{
public:
int m_a;
virtual void operator =(const A &other) {m_a = other.m_a;}
virtual void operator+=(const A &other){m_a += other.m_a;}
}

but this will not work, since the interefaces are different, A v.s.
Interace_
So a solution I can think is to use

class Interface_
{
public:
virtual void operator = (const Interface *other)=0;
virtual void operator +=(const Interface *other)=0;
}

but in the sub class
class A:public Interface_
{
public:
int m_a;
virtual void operator =(const _Interface *other) {m_a =
((A*)(other))->m_a;}
virtual void operator+=(const _Interface *other){m_a +=
((A*)(other))->m_a;}
};

But this looks not clean, because of the pointer conversion, it is not
safe,
Is there better solution?

Thanks in advance!

Mar 5 '06 #1
2 3059
ww*******@gmail.com wrote:
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
class A:public Interface_
{
public:
int m_a;
virtual void operator =(const A &other) {m_a = other.m_a;}
virtual void operator+=(const A &other){m_a += other.m_a;}
}

but this will not work, since the interefaces are different, A v.s.
Interace_
So a solution I can think is to use

class Interface_
{
public:
virtual void operator = (const Interface *other)=0;
Did you mean

virtual void operator = (const Interface_ *other) = 0;

??? And what's the actual point of passing a _pointer_? You can't
really use it in any decent expression that way... You might think
of passing a _reference_:

virtual void operator = (const Interface_ & other) = 0;
virtual void operator +=(const Interface *other)=0;
Same here: a typo in the argument type and a strong recommendation
to use a reference instead of a pointer.
}

but in the sub class
class A:public Interface_
{
public:
int m_a;
virtual void operator =(const _Interface *other) {m_a =
((A*)(other))->m_a;}
virtual void operator+=(const _Interface *other){m_a +=
((A*)(other))->m_a;}
};

But this looks not clean, because of the pointer conversion, it is not
safe,
Is there better solution?


It's not just "not clean", it's utterly dangerous. What if I derive
'B' from your 'Interface_' and shove it as the argument to 'A::op +='?
Your cast will try to convert my 'B' to your 'A', which it definitely
*isn't*.

One advice: don't do it. Other advice: use 'dynamic_cast':

virtual void operator=(const Interface_ & other) {
try {
A& a = dynamic_cast<A&>(other);
blahblahblah;
} catch (std::bad_cast&) {
// not an object of type 'A' passed in
}
}

V
--
Please remove capital As from my address when replying by mail
Mar 5 '06 #2
Drop the abstract Interface (or _Interface or Interface_ as appeared in
your code to represent presumably the same entity) class and everything
will work fine.

Usually, operator overloading is for concrete classes. It is even
difficult to user operator on a pointer to abstract class since you need
to fill in a lot of dereferencing operators as well:

A a;
Interface* i = &a;

a += a;
(*i) += (*i); // smelly

Regards,
Ben
Mar 6 '06 #3

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

Similar topics

5
by: bsaucer | last post by:
I am creating a class with operator overloads. It makes references to another class I've created, but do not wish to modify. I try to overload an operator having arguments having the other class...
21
by: Jon Slaughter | last post by:
I have a class that is basicaly duplicated throughout several files with only members names changing according to the class name yet with virtually the exact same coding going on. e.g. class...
8
by: Floogle | last post by:
how do i create a virtual == operator. I've tried the following but it's incorrect... class Interface { ... public: virtual bool operator==(const Interface& rhs)const=0;
10
by: shachar | last post by:
hi all. can i OverLoad Operators - such as Exclamation Mark "!" ? how?
9
by: Tony | last post by:
I have an operator== overload that compares two items and returns a new class as the result of the comparison (instead of the normal bool) I then get an ambiguous operater compile error when I...
3
by: needin4mation | last post by:
The code is taken from the book Professional C#: abstract class GenericCustomer { private string name; public GenericCustomer(string name) { this.name = name; }...
15
by: Jeroen | last post by:
Hi all, I've got a very specific question about the evaluation order in C++. Assume some kind of custom array class, with an overloaded subscript operator. In the following code: { my_array...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
4
by: t f | last post by:
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;}}
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: 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...
0
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,...
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
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,...
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
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...
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.