To inherit or not, that is the question

  • Thread starter =?iso-8859-1?q?Erik_Wikstr=F6m?=
  • Start date
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

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?
 
I

Ian Collins

Erik said:
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.
 
?

=?iso-8859-1?q?Kirit_S=E6lensminde?=

Erik said:
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
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top