abstract class question

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;
}
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;}
}

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

class Interface_
{
public:
virtual void operator = (const Interface *other)=0;
virtual void operator +=(const Interface *other)=0;
}
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!
 
G

Greg

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;
}
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;}
}

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

class Interface_
{
public:
virtual void operator = (const Interface *other)=0;
virtual void operator +=(const Interface *other)=0;
}
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?

Why bother? If the overloaded operator is not implemented, than any
code that would have used the overloaded operator had it been
implemented, will fail to compile when no overloaded operator is found.


And if the overloaded operator is not used anywhere in the program, why
waste time requiring that an implementation be written anyway, even
though it will not be called?

Greg
 
V

Victor Bazarov

Greg said:
I want to enfore the implementation of the operator overloading eg
[..]

Why bother? If the overloaded operator is not implemented, than any
code that would have used the overloaded operator had it been
implemented, will fail to compile when no overloaded operator is
found.


And if the overloaded operator is not used anywhere in the program,
why waste time requiring that an implementation be written anyway,
even though it will not be called?

If the class does not declare/define a final overrider for a pure
function, such class cannot be instantiated.

V
 
G

Greg

Victor said:
Greg said:
I want to enfore the implementation of the operator overloading eg
[..]

Why bother? If the overloaded operator is not implemented, than any
code that would have used the overloaded operator had it been
implemented, will fail to compile when no overloaded operator is
found.


And if the overloaded operator is not used anywhere in the program,
why waste time requiring that an implementation be written anyway,
even though it will not be called?

If the class does not declare/define a final overrider for a pure
function, such class cannot be instantiated.

Granted, but I'm saying that using an abstract method to force the
implementation of the overloaded operator is superfluous here. The user
will be forced to write the routine anyway - if it is at all needed.

In the event that the overloaded operator is left unimplemented then
any expression that would use the operator will fail to compile. So to
be able to build the program successfully, the user will have to
implement the referenced - but unimplemented - overloaded operator -
and will have to do so whether or not an abstract class exists that
compels the same.

Greg
 
W

www.brook

If abstract class is used, error could happen at run time if you use
pointers.
eg

interface_ * a, * b,

you do not know what actually pointed by a and b
when you call
*a += *b,
if the base class implemented the operator +=, but the subclass did
not, wired things could happen.
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top