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

To inherit or not, that is the question

I'm working on a project where I need a couple of classes that purely
conceptually are related to each other such that I could create a
base-class and have a is-a relation between each of the classes and the
base-class. However each class is also extremely specialized for a
special purpose and there should be few if any instances where one
would be able to exchange one class for another, due to their very
specialized nature.

Further more, if I did create a common base-class it would either have
to be very minimal giving me only virtual functions for the most
trivial ones, or I could mandate a specific interface that each class
have to follow but that would require lots of extra work, since I would
then have to implement functions in some classes that will never
(should never even) be used and lose out on some optimization-points
(performance is of great importance and these classes (containers
holding up to millions of elements) are right in the critical path).
Either way (small base-class or large) there won't be much code that
can be put in the base-class since they are all so different on the
inside.

Currently I'm using template-functions in all cases where more than one
class can be used as a reference and it's working fine. It even
allowing me more freedom since I'm not bound to have the same
return-type on a method in one class as in another (I can use
proxy-objects), which would not be possible with inheritance, unless
the base-class was very small.

The only problem is that deep inside there is a voice (brought up on
Java) telling me that this would make a great class-hierarchy. Someone
with more wisdom than me, please help me, how far from the OO-path is
one allowed to stray for the sake of efficiency and ease of
programming?

--
Erik Wikström

Dec 14 '06 #1
2 1173
Erik Wikström wrote:
I'm working on a project where I need a couple of classes that purely
conceptually are related to each other such that I could create a
base-class and have a is-a relation between each of the classes and the
base-class. However each class is also extremely specialized for a
special purpose and there should be few if any instances where one
would be able to exchange one class for another, due to their very
specialized nature.
I think this answers your question for you! C++ provides a good
solution for this kind of situation, namely:
>
Currently I'm using template-functions in all cases where more than one
class can be used as a reference and it's working fine. It even
allowing me more freedom since I'm not bound to have the same
return-type on a method in one class as in another (I can use
proxy-objects), which would not be possible with inheritance, unless
the base-class was very small.

The only problem is that deep inside there is a voice (brought up on
Java) telling me that this would make a great class-hierarchy.
Banish the voice.

--
Ian Collins.
Dec 14 '06 #2

Erik Wikström wrote:
I'm working on a project where I need a couple of classes that purely
conceptually are related to each other such that I could create a
base-class and have a is-a relation between each of the classes and the
base-class. However each class is also extremely specialized for a
special purpose and there should be few if any instances where one
would be able to exchange one class for another, due to their very
specialized nature.

Further more, if I did create a common base-class it would either have
to be very minimal giving me only virtual functions for the most
trivial ones, or I could mandate a specific interface that each class
have to follow but that would require lots of extra work, since I would
then have to implement functions in some classes that will never
(should never even) be used and lose out on some optimization-points
(performance is of great importance and these classes (containers
holding up to millions of elements) are right in the critical path).
Either way (small base-class or large) there won't be much code that
can be put in the base-class since they are all so different on the
inside.

Currently I'm using template-functions in all cases where more than one
class can be used as a reference and it's working fine. It even
allowing me more freedom since I'm not bound to have the same
return-type on a method in one class as in another (I can use
proxy-objects), which would not be possible with inheritance, unless
the base-class was very small.

The only problem is that deep inside there is a voice (brought up on
Java) telling me that this would make a great class-hierarchy. Someone
with more wisdom than me, please help me, how far from the OO-path is
one allowed to stray for the sake of efficiency and ease of
programming?

As Ian Collins says, banish the voice.

Here are three constructs that are clearly closely related, but we
probably wouldn't ever want a common super-class:

char string1[ 50 ];
std::string string2;
std::vector< char string3;

That doesn't mean we wouldn't expect to use the same (std::) algorithms
on them though, which is done in exactly the way that you are doing it,
through templates.

Java is a slightly odd OO language in that it is both strongly typed
*and* imposes severe restrictions on defining those types.

I've never taught is-a or has-a when teaching OO. has-a isn't so bad,
but is-a leads to so many ill-conceived hierarchies that I tell people
to never think about hierarchies in that way. The hierarchy is there to
serve a purpose in the code, not to document some notion of taxonomy
from the problem domain.
K

Dec 14 '06 #3

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

Similar topics

2
by: Adam | last post by:
I am trying to inherit from MessageBox and i get error in compilation time. The code is: public class MyMessageBox : MessageBox { public static DialogResult Show(int errorCode) { return...
7
by: Fabian Neumann | last post by:
Hi! I got a problem with font-family inheritance. Let's say I have CSS definitions like: p { font:normal 10pt Verdana; } strong { font:normal 14pt inherit;
1
by: Jeff Schmidt | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello, ~ Ok, I've got some tables, and a style sheet that contains stuff to format the table. The problem I'm having is, it appears that using...
1
by: kyo guan | last post by:
How to Adding Functionality to a Class by metaclass(not by inherit) #example: import inspect class Foo(object): def f(self): pass
5
by: john conwell | last post by:
I'm trying to make a class that already inherits from a base class, also inherit from an interface in managed C++. so my interface looks like so: __gc interface ITask { __property String*...
7
by: Frank | last post by:
Hi, a question probably asked before, but I can't find the answers. Base class X, classes A, B and C inherit class X. In class A I do not want to inherit property (or function or method) P1....
4
by: Hardy Wang | last post by:
Hi, I have two classes (ClassA and ClassB), public ClassA { public ClassA() { this.MyButton.Clicked += new System.EventHandler(this.MyButton_Click); } private void MyButton_Click(object...
3
by: J | last post by:
I tried to inherit 'Shot' class from 'Image' class, only to fail. It gives me the CS0122 error, which says that it can't access 'System.Drawing.Image.Image()'. What am I missing? using...
3
by: jefftyzzer | last post by:
Friends: I have a stored procedure declared as: CREATE PROCEDURE X.Y LANGUAGE SQL DYNAMIC RESULT SETS 1 READS SQL DATA SPECIFIC Y INHERIT SPECIAL REGISTERS
2
by: GTalbot | last post by:
Hello fellow comp.infosystems.www.authoring.stylesheets colleagues, Imagine this situation: #grand-parent-abs-pos { height: 400px; position: absolute; width: 600px; }
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.