Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Should 'public virtual' always become 'private virtual'? & using private inheritance (http://www.velocityreviews.com/forums/t281051-should-public-virtual-always-become-private-virtual-and-using-private-inheritance.html)

qazmlp 01-31-2004 03:08 PM

Should 'public virtual' always become 'private virtual'? & using private inheritance
 
class base
{
// other members
public:
virtual ~base()
{
}
virtual void virtualMethod1()=0 ;
virtual void virtualMethod2()=0 ;
virtual void virtualMethod3()=0 ;
} ;


I derive a class from base. I want to make all the virtual functions
as private.
class derived : public base
{
// other members
public:
virtual ~derived()
{
}

void myPublicInterface1() ;
void myPublicInterface1() ;

private:
virtual void virtualMethod1()=0 ;
virtual void virtualMethod2()=0 ;
virtual void virtualMethod3()=0 ;
} ;


I did the above considering the facts that
- in any case, the virtual functions will always be called through
the base class pointer only
- No. of public virtual interfaces should be reduced as much as
possible

Here are my questions:
1)
Can I consider this as a good design? If yes/no, why?

2)
How about
class derived : private base
{

}
instead of the above design, as anyway, all the inherited virtual
functions need to be made as private ?

Is this a correct design? If yes/no, why?


3)
What are the flaws in the above design and what do you suggest for
improvements?

Peter Koch Larsen 01-31-2004 03:18 PM

Re: Should 'public virtual' always become 'private virtual'? & using private inheritance
 

"qazmlp" <qazmlp1209@rediffmail.com> skrev i en meddelelse
news:db9bbf31.0401310708.53fa270e@posting.google.c om...
> class base
> {
> // other members
> public:
> virtual ~base()
> {
> }
> virtual void virtualMethod1()=0 ;
> virtual void virtualMethod2()=0 ;
> virtual void virtualMethod3()=0 ;
> } ;
>
>
> I derive a class from base. I want to make all the virtual functions
> as private.
> class derived : public base
> {
> // other members
> public:
> virtual ~derived()
> {
> }
>
> void myPublicInterface1() ;
> void myPublicInterface1() ;
>
> private:
> virtual void virtualMethod1()=0 ;
> virtual void virtualMethod2()=0 ;
> virtual void virtualMethod3()=0 ;
> } ;


This has absolutely no value. Those functions are accesible anyway, you
know.

>
>
> I did the above considering the facts that
> - in any case, the virtual functions will always be called through
> the base class pointer only
> - No. of public virtual interfaces should be reduced as much as
> possible
>
> Here are my questions:
> 1)
> Can I consider this as a good design? If yes/no, why?


No - because you do not reduce the number of public, virtual functions.
>
> 2)
> How about
> class derived : private base
> {
>
> }
> instead of the above design, as anyway, all the inherited virtual
> functions need to be made as private ?
>
> Is this a correct design? If yes/no, why?


This is not any better as base *b = new derived(...) will no longer compile.

>
>
> 3)
> What are the flaws in the above design and what do you suggest for
> improvements?


Use this scheme instead:
class base
{
// other members
virtual void virtualMethod1()=0 ;
virtual void virtualMethod2()=0 ;
virtual void virtualMethod3()=0 ;
public:
virtual ~base()
{
}
void method1() { virtualMethod1();}
void method2() { virtualMethod2();}
void method3() { virtualMethod3();}

};




Nick Hounsome 01-31-2004 07:07 PM

Re: Should 'public virtual' always become 'private virtual'? & using private inheritance
 

"qazmlp" <qazmlp1209@rediffmail.com> wrote in message
news:db9bbf31.0401310708.53fa270e@posting.google.c om...
> class base
> {
> // other members
> public:
> virtual ~base()
> {
> }
> virtual void virtualMethod1()=0 ;
> virtual void virtualMethod2()=0 ;
> virtual void virtualMethod3()=0 ;
> } ;
>
>
> I derive a class from base. I want to make all the virtual functions
> as private.
> class derived : public base
> {
> // other members
> public:
> virtual ~derived()
> {
> }
>
> void myPublicInterface1() ;
> void myPublicInterface1() ;
>
> private:
> virtual void virtualMethod1()=0 ;
> virtual void virtualMethod2()=0 ;
> virtual void virtualMethod3()=0 ;
> } ;
>
>
> I did the above considering the facts that
> - in any case, the virtual functions will always be called through
> the base class pointer only
> - No. of public virtual interfaces should be reduced as much as
> possible
>
> Here are my questions:
> 1)
> Can I consider this as a good design? If yes/no, why?
>


Because public inheritance should mean IS-A which implies that
you must provide at least as much if not more than the base class.

> 2)
> How about
> class derived : private base
> {
>
> }
> instead of the above design, as anyway, all the inherited virtual
> functions need to be made as private ?
>
> Is this a correct design? If yes/no, why?
>


Depends - they can't both be right because either a derived IS-A base or not
and
that depends on what derived and base actually are.

>
> 3)
> What are the flaws in the above design and what do you suggest for
> improvements?


It's not a design at all because it doesn't define what base and derived
actually are.
Until you decide that you cannot make sensible decisions about how to
implement them.

You must either tell us what they are supposed to be or rewrite your
question as
one of a purely language nature rather than design.



Joe 01-31-2004 10:25 PM

Re: Should 'public virtual' always become 'private virtual'? & using private inheritance
 
Just curious......what is the advantage of lowering the number of public
virtual methods? Thanks


"Peter Koch Larsen" <pklspam@mailme.dk> wrote in message
news:HGPSb.81747$jf4.5236142@news000.worldonline.d k...
>
> "qazmlp" <qazmlp1209@rediffmail.com> skrev i en meddelelse
> news:db9bbf31.0401310708.53fa270e@posting.google.c om...
> > class base
> > {
> > // other members
> > public:
> > virtual ~base()
> > {
> > }
> > virtual void virtualMethod1()=0 ;
> > virtual void virtualMethod2()=0 ;
> > virtual void virtualMethod3()=0 ;
> > } ;
> >
> >
> > I derive a class from base. I want to make all the virtual functions
> > as private.
> > class derived : public base
> > {
> > // other members
> > public:
> > virtual ~derived()
> > {
> > }
> >
> > void myPublicInterface1() ;
> > void myPublicInterface1() ;
> >
> > private:
> > virtual void virtualMethod1()=0 ;
> > virtual void virtualMethod2()=0 ;
> > virtual void virtualMethod3()=0 ;
> > } ;

>
> This has absolutely no value. Those functions are accesible anyway, you
> know.
>
> >
> >
> > I did the above considering the facts that
> > - in any case, the virtual functions will always be called through
> > the base class pointer only
> > - No. of public virtual interfaces should be reduced as much as
> > possible
> >
> > Here are my questions:
> > 1)
> > Can I consider this as a good design? If yes/no, why?

>
> No - because you do not reduce the number of public, virtual functions.
> >
> > 2)
> > How about
> > class derived : private base
> > {
> >
> > }
> > instead of the above design, as anyway, all the inherited virtual
> > functions need to be made as private ?
> >
> > Is this a correct design? If yes/no, why?

>
> This is not any better as base *b = new derived(...) will no longer

compile.
>
> >
> >
> > 3)
> > What are the flaws in the above design and what do you suggest for
> > improvements?

>
> Use this scheme instead:
> class base
> {
> // other members
> virtual void virtualMethod1()=0 ;
> virtual void virtualMethod2()=0 ;
> virtual void virtualMethod3()=0 ;
> public:
> virtual ~base()
> {
> }
> void method1() { virtualMethod1();}
> void method2() { virtualMethod2();}
> void method3() { virtualMethod3();}
>
> };
>
>
>




Phlip 01-31-2004 11:22 PM

Re: Should 'public virtual' always become 'private virtual'? & using private inheritance
 
Joe wrote:

> Just curious......what is the advantage of lowering the number of public
> virtual methods? Thanks


Liskov Substitution Principle gets easier to enforce - admitedly by
attrition, not necessarily thinking.

--
Phlip
http://www.xpsd.org/cgi-bin/wiki?Tes...UserInterfaces



Rob 02-01-2004 01:07 AM

Re: Should 'public virtual' always become 'private virtual'? & using private inheritance
 
"Joe" <jblack@wowmail.com> wrote in message
news:UXVSb.16547$gw3.7912626@news4.srv.hcvlny.cv.n et...
> Just curious......what is the advantage of lowering the number of public
> virtual methods? Thanks
>


The most usual reason is that a public virtual function (virtually by
definition :-)
can be called by anyone, and the developer who overrides it must
therefore do things such as checking arguments to make sure they
are valid, and that the affected object remains in a valid state. And it
is easier to make a mistake and forget to check something.

Private virtual functions do not have that problem. They can only be
called by member functions or friends of the base class. This means
that the implementer of the base class can control the conditions under
which the virtual function is called. In this way, necessary preconditions
and postconditions can be checked and/or enforced.



Rod Davison 02-01-2004 01:45 AM

Re: Should 'public virtual' always become 'private virtual'? & using private inheritance
 
On Sat, 31 Jan 2004 07:08:44 -0800, qazmlp wrote:

> class base
> {
> // other members
> public:
> virtual ~base()
> {
> }
> virtual void virtualMethod1()=0 ;
> virtual void virtualMethod2()=0 ;
> virtual void virtualMethod3()=0 ;
> } ;
>
>
> I derive a class from base. I want to make all the virtual functions as
> private.



Aside from not being allowed in some OO languages, it is always a bad
design decision to have a derived class remove an operation from the base
class definition, which is essentially what making a base class public
operation private in the derived class. Two reasons:

1. The derived class should not break the base class contract -- which is
what Liskov's principle is all about.

2. Derived classes should extend base classes, not reduce them -- this
suggests a potential design problem with the inheritance hierarchy.

Practical quick fix (but not necessarily a good one). Make the base class
virtual methods protected.

Also from a practical point of view, if your code compiles, are you really
sure that it does what think it does?

--
..................................................
The three most dangerous things are a programmer
with a soldering iron, a manager who codes, and a user who gets ideas.

Rod Davison - Critical Knowledge Systems Inc.


Rod Davison 02-01-2004 02:01 AM

Re: Should 'public virtual' always become 'private virtual'? & using private inheritance
 

> Also from a practical point of view, if your code compiles, are you really
> sure that it does what think it does?


Looking at the code I suspect that there will be a problem with the vtab
setup.
--
..................................................
If you lend someone $20, and never see that person
again, it was probably worth it.

Rod Davison - Critical Knowledge Systems Inc.


Daniel T. 02-01-2004 07:37 PM

Re: Should 'public virtual' always become 'private virtual'? & using private inheritance
 
qazmlp1209@rediffmail.com (qazmlp) wrote:

> class base
> {
> // other members
> public:
> virtual ~base()
> {
> }
> virtual void virtualMethod1()=0 ;
> virtual void virtualMethod2()=0 ;
> virtual void virtualMethod3()=0 ;
> } ;
>
>
> I derive a class from base. I want to make all the virtual functions
> as private.
> class derived : public base
> {
> // other members
> public:
> virtual ~derived()
> {
> }
>
> void myPublicInterface1() ;
> void myPublicInterface1() ;
>
> private:
> virtual void virtualMethod1()=0 ;
> virtual void virtualMethod2()=0 ;
> virtual void virtualMethod3()=0 ;
> } ;
>
>
> I did the above considering the facts that
> - in any case, the virtual functions will always be called through
> the base class pointer only


Is this true? Will it always be true? Let's say I have a function with a
derived* and I want to call virtualMaethod1.

void func( derived* d ) {
static_cast<base*>(d)->virtualMethod1();
}

It can be done, by why are you forcing the client to jump through the
extra hoop?


> - No. of public virtual interfaces should be reduced as much as
> possible


You haven't reduced the number of virtual interfaces in 'derived' with
the above code.


> Here are my questions:
> 1)
> Can I consider this as a good design? If yes/no, why?


No, for the reasons cited above.


> 2)
> How about
> class derived : private base
> {
>
> }
> instead of the above design, as anyway, all the inherited virtual
> functions need to be made as private ?
>
> Is this a correct design? If yes/no, why?


If you do this, you can't hold a derived object in a base*. Is that what
you want?

> 3)
> What are the flaws in the above design and what do you suggest for
> improvements?


I see two flaws in the above design. (a) you are attempting to make
private in derived that which cannot be made private because it is
public in the base class. (b) I don't know what in in 'other members' in
your base class, but I suspect I would not approve of whatever it is...

Uncle Bob (Robert C. Martin) 02-02-2004 11:19 PM

Re: Should 'public virtual' always become 'private virtual'? & using private inheritance
 
qazmlp1209@rediffmail.com (qazmlp) might (or might not) have written
this on (or about) 31 Jan 2004 07:08:44 -0800, :

>class base
>{
> // other members
> public:
> virtual ~base()
> {
> }
> virtual void virtualMethod1()=0 ;
> virtual void virtualMethod2()=0 ;
> virtual void virtualMethod3()=0 ;
>} ;
>
>
>I derive a class from base. I want to make all the virtual functions
>as private.
>class derived : public base
>{
> // other members
> public:
> virtual ~derived()
> {
> }
>
> void myPublicInterface1() ;
> void myPublicInterface1() ;
>
> private:
> virtual void virtualMethod1()=0 ;
> virtual void virtualMethod2()=0 ;
> virtual void virtualMethod3()=0 ;
>} ;
>
>
>I did the above considering the facts that
> - in any case, the virtual functions will always be called through
>the base class pointer only
> - No. of public virtual interfaces should be reduced as much as
>possible
>
>Here are my questions:
>1)
>Can I consider this as a good design? If yes/no, why?


No, probably because it won't compile. You can't make a derivative
member less accessible than a base member.
>
>2)
>How about
>class derived : private base
>{
>
>}
>instead of the above design, as anyway, all the inherited virtual
>functions need to be made as private ?
>
>Is this a correct design? If yes/no, why?


This is better if the intent is for all the methods of derived to be
inaccessible to users of derived. However you also have to remember
that private inheritance prevents the implicit upcast from derived* to
base*

>3)
>What are the flaws in the above design and what do you suggest for
>improvements?


That depends on your intent. I think 2) is better than 1).



Robert C. Martin | "Uncle Bob"
Object Mentor Inc. | unclebob @ objectmentor . com
501 N. Riverside Dr.| Tel: (800) 338-6716
Suite 206 | Fax: (847) 775-8174 | www.objectmentor.com
| | www.XProgramming.com
Gurnee, IL, | Training and Mentoring | www.junit.org
60031 | OO, XP, Agile, C++, Java, C# | http://fitnesse.org


All times are GMT. The time now is 10:58 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.