To inherit or not, that is the question

  • Follow


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=F6m

0
Reply eriwik (500) 12/14/2006 8:21:18 AM

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.
0
Reply ian-news (9878) 12/14/2006 8:52:57 AM


Erik Wikstr=F6m 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

0
Reply kirit.saelensminde (164) 12/14/2006 10:27:02 AM

2 Replies
15 Views

(page loaded in 0.081 seconds)


Reply: