C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

To inherit or not, that is the question

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Erik Wikström
Guest





PostPosted: Thu Dec 14, 2006 10:11 am    Post subject: To inherit or not, that is the question Reply with quote



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
Back to top
Ian Collins
Guest





PostPosted: Thu Dec 14, 2006 10:11 am    Post subject: Re: To inherit or not, that is the question Reply with quote



Erik Wikström wrote:
Quote:
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:
Quote:

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.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.