473,396 Members | 1,804 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,396 software developers and data experts.

abstract class question

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!

Mar 5 '06 #1
5 1680
ww*******@gmail.com wrote:
[...]


'comp.lang.c++' is not a chat-room, nor is it an IRC channel.
Post and _wait_ for replies. Patience is a virtue.

V
--
Please remove capital As from my address when replying by mail
Mar 5 '06 #2
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;
}
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

Mar 6 '06 #3
Greg wrote:
ww*******@gmail.com wrote:
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
--
Please remove capital As from my address when replying by mail
Mar 6 '06 #4
Victor Bazarov wrote:
Greg wrote:
ww*******@gmail.com wrote:
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

Mar 6 '06 #5
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.

Mar 7 '06 #6

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

Similar topics

3
by: Murat Tasan | last post by:
so here is another general question about java... why can't you declare an abstract static method. i can envision the case (indeed i have experienced the case) where one would want an...
17
by: Medi Montaseri | last post by:
Hi, Given a collection of similar but not exact entities (or products) Toyota, Ford, Buick, etc; I am contemplating using the Abstraction pattern to provide a common interface to these products....
10
by: Joe | last post by:
My question is more an OOD question. I know *how* to implement both abstract classes and interfaces. Here's my question - under what circumstacnes does one use an abstract class and under what...
9
by: WithPit | last post by:
I am trying to create an Managed C++ Wrapper around an unmanaged library which contains C++ code. Some of the unmanaged methods returns an returntype which is of the abstract base type (for...
17
by: baibaichen | last post by:
i have written some code to verify how to disable slicing copy according C++ Gotchas item 30 the follow is my class hierarchy, and note that B is abstract class!! class B { public: explicit...
5
by: Tony Johansson | last post by:
Hello!! Assume you have an Interface called ITest with these three method declarations. interface ITest { void foo1(); void foo2(); void foo3(); }
7
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears...
4
by: David Zha0 | last post by:
Hi, "when we call a virtual method, the runtime will check the instance who called the method and then choose the suitable override method, this may causes the performance drop down", is this...
52
by: Ben Voigt [C++ MVP] | last post by:
I get C:\Programming\LTM\devtools\UselessJunkForDissassembly\Class1.cs(360,27): error CS0535: 'UselessJunkForDissassembly.InvocableInternals' does not implement interface member...
5
by: Tony Johansson | last post by:
Hello! Here I have an Interface called ITest and a class called MyClass which derive this intrface. As you can see I don't implement this method myTest in class MyClass because i use the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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
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,...

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.