| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"" |
| Date: |
05 Mar 2006 01:46:06 PM |
| Object: |
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!
.
|
|
| User: "Victor Bazarov" |
|
| Title: Re: question about operator overload and abstract class |
05 Mar 2006 04:54:50 PM |
|
|
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
.
|
|
|
|
| User: "benben" |
|
| Title: Re: question about operator overload and abstract class |
06 Mar 2006 01:05:26 AM |
|
|
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
.
|
|
|
|

|
Related Articles |
|
|