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 

Public Inheritance issues

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
RenjithMohan
Guest





PostPosted: Tue Aug 23, 2005 11:47 pm    Post subject: Public Inheritance issues Reply with quote



It has been stated in umpteen places that Public Inheritance should
only be used
1) When there is an abstraction which can be modeled as an interface
2) When there is Subsitutablity ( LSP)

The thing I am concerned is whether these are the only cases that I can
use Public Inheritance.
The situation goes like this . I have several DB classes in C++ which
share some code like the Open , Close , IsOpen functions.So to enable
this Design

I create a base class which implements these functions and all my other
Derived DB classes inherit publically from this since clients of these
classes would be calling these functions
class BaseDB
{
void Open();
void Close();
void IsOpen();
const char* GetError();
};

class ClientDB : public BaseDB
{
void GetClientData();
....
};
class MMDB : public BaseDB
{
bool CheckIfClaimExists();
};

The problem we are facing is that we know that there are some clients
which just use MMDB and ClinteDB without using the BaseDB . So would it
be worthwhile to write stub functions in all such DB classes and
contain the BaseDB or to use public inheritance to avoid writing such
stub code.

What is inherently wrong in this design from a pragmatic point of view?.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Carl Barron
Guest





PostPosted: Wed Aug 24, 2005 8:07 am    Post subject: Re: Public Inheritance issues Reply with quote



In article <1124813382.064790.106980 (AT) g44g2000cwa (DOT) googlegroups.com>,
RenjithMohan <renjithmohan (AT) hotmail (DOT) com> wrote:

Quote:
It has been stated in umpteen places that Public Inheritance should
only be used
1) When there is an abstraction which can be modeled as an interface
2) When there is Subsitutablity ( LSP)

The thing I am concerned is whether these are the only cases that I can
use Public Inheritance.
The situation goes like this . I have several DB classes in C++ which
share some code like the Open , Close , IsOpen functions.So to enable
this Design

I create a base class which implements these functions and all my other
Derived DB classes inherit publically from this since clients of these
classes would be calling these functions
class BaseDB
{
void Open();
void Close();
void IsOpen();
const char* GetError();
};

class ClientDB : public BaseDB
{
void GetClientData();
...
};
class MMDB : public BaseDB
{
bool CheckIfClaimExists();
};

The problem we are facing is that we know that there are some clients
which just use MMDB and ClinteDB without using the BaseDB . So would it
be worthwhile to write stub functions in all such DB classes and
contain the BaseDB or to use public inheritance to avoid writing such
stub code.

What is inherently wrong in this design from a pragmatic point of view?.

It confuses those who see inheritance == runtime polymorphism.

Outside of that I see no real problem. I see no problem with it as
long
as runtime polymorphism is ruled out.
Perhaps private inheritance of the basic operations class is
appropriate as it looks like a 'has a' relationship. ConcreteDataBase
'has a' BasicOperationsHandler. This seems closer to your design than
any 'is a' relationship.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
tony_in_da_uk@yahoo.co.uk
Guest





PostPosted: Wed Aug 24, 2005 12:47 pm    Post subject: Re: Public Inheritance issues Reply with quote



The guidelines you mention are more applicable to situations with
polymorphic hierarchies where the client is regularly passing derived
types into functions that rely on the relationship with the base class.
These functions, if poorly implemented, can cause you trouble. For
example, by updating only the base-class component during a by-value
copy. Still, in your situation the clients will be dealing with very
few objects, and are unlikely to routinely apply hacked-together
polymorphic algorithms to them, so there's little risk.

Separately, there is a potential physical design argument for
insulating the client from changes to the header defining BaseDB, but
it would be a permature optimisation to write forwarding functions
unless that header pulled in a lot of other headers, and had an
avoidable adverse affect on client compilation times.

Cheers, Tony


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
RenjithMohan
Guest





PostPosted: Wed Aug 24, 2005 10:25 pm    Post subject: Re: Public Inheritance issues Reply with quote

Actually in this case, the BaseDB represents a convenient set of
functions to call into the DB API layer which is ADO in this case. So
changing BaseDB should in fact affect all its siblings since they all
depend on the DB API layer.
So the question actually is, in the absence of polymorphic functions
should we use public inheritance against community wisdom or deal with
not too helpful forwarding functions just for the sake for "stronger
encapsulation" ?.
I didnt see this case listed as an exceptional situation to use public
inheritance in the absence of polymorphic functions .
Quote:
From a abstract point of view I realize completely that I am using a
default implementation and an interface instead of just an interface

as the advice goes.
This involves tight coupling etc.
But what if I do have a good default implementation ?.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Joe
Guest





PostPosted: Wed Aug 24, 2005 10:26 pm    Post subject: Re: Public Inheritance issues Reply with quote

In this situation, I would consider using private inheritance and
exposing the functions explicitly with 'using' statements.

class ClientDB : private BaseDB
{
public:
using BaseDb::Open;
using BaseDb::Close;
using BaseDb::IsOpen;
using BaseDb::GetError;
}

This for requires a little more typing, but it prevents accidental use
of the polymorphism and makes explicit the pieces you wish to expose
from BaseDB.

joe


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Tony Delroy
Guest





PostPosted: Sat Aug 27, 2005 10:15 am    Post subject: Re: Public Inheritance issues Reply with quote

Quote:
[...] changing BaseDB should in fact affect all its siblings since
they all depend on the DB API layer.

What siblings does BaseDB have? A change to BaseDB will affect its
users, be they direct users, derived classes or users of derived
classes. The benefit of public inheritance is that many changes to
BaseDB are _automatically_ reflected in derived classes (changes to
stuff explicitly used in the derived class may still necessitate
edits). The side effect is the "is-a" relationship, which may be
useful sometimes, and may be abused sometimes.

Joe's advice is reasonable... with only the effort of having to
maintain an explicit "using" list in BaseDB you get the cleanness that
you prefer. Chances are the changes to BaseDB's direct and derive
class users will be so much larger that this maintenance burden becomes
insignificant.

Still, my own opinion is that the dangers of someone abusing public
inheritance rises with the attempts to benefit from the possible
polymorphic behaviours. I would imagine that the users of your
ClientDB, MMDB etc are unlikely to find a use for such polymorphism,
making the danger of abuse insignificant.

So choose between insignificant maintenance burden or insignificant
polymorphic support and danger as you see fit...

Regards,

Tony


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) 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.