question about operator overload and abstract class

W

www.brook

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!
 
V

Victor Bazarov

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::eek:p +='?
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
 
B

benben

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,785
Messages
2,569,624
Members
45,318
Latest member
LuisWestma

Latest Threads

Top