473,398 Members | 2,165 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

The private-inheritance variant allows Car to override Engine's virtual functions

The statement is taken from FAQ [24.2]. What about non-virtual
functions? Can they be overriden? I still don't see a good
justification to prefer private inheritance over composition. In
fact, I have never seen it in a commercial code. If someone did,
please share the use-case and decisions behind it.
Thanks

Sep 28 '07 #1
8 2019
puzzlecracker wrote:
The statement is taken from FAQ [24.2]. What about non-virtual
functions? Can they be overriden?
No. The term "override" relates only to virtual functions. Non-
virtual functions are either inherited or hidden. They can also
be overloaded if brought into the derived class' scope with the
help of a 'using' declaration.
I still don't see a good
justification to prefer private inheritance over composition. In
fact, I have never seen it in a commercial code. If someone did,
please share the use-case and decisions behind it.
I am too lazy to search through our multimillion-LOC codebase,
and there is no guarantee I'd find anything. If you don't see
a good justification now, you won't see any justification given
to you as good, either. Goodness is in the eye of the beholder.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 28 '07 #2
On Sep 28, 6:30 am, puzzlecracker <ironsel2...@gmail.comwrote:
The statement is taken from FAQ [24.2]. What about non-virtual
functions? Can they be overriden? I still don't see a good
justification to prefer private inheritance over composition. In
fact, I have never seen it in a commercial code. If someone did,
please share the use-case and decisions behind it.

Thanks
I think it's fair to say that there's nothing that can be done with
private inheritance that can't be done with aggregation/composition.
The usual justification is that you want to aggregate a class and
override some virtual methods that it provides. Consider a class that
will use a timer. The timer has a virtual method OnTick which is
called every n seconds as defined in the class.
You could derive a class and override the OnTick method to do
something specific for your class and then aggregate that derivation.
Or, you could inherit privately and override OnTick within your class
giving you slightly/arguably cleaner code.

Sep 28 '07 #3
On Sep 28, 6:30 am, puzzlecracker <ironsel2...@gmail.comwrote:
The statement is taken from FAQ [24.2]. What about non-virtual
functions? Can they be overriden? I still don't see a good
justification to prefer private inheritance over composition. In
fact, I have never seen it in a commercial code. If someone did,
please share the use-case and decisions behind it.

Thanks
AFAIK one reason to prefer private inheritance over
composition is to simplify delegation: eg:

class Embedee {
public:
void foo();
int bar(double);
char* baz(int);
void dont_want_to_expose_this_one();
};
class Agreggator : private Embedee {
public:
using Embedee::foo;
using Embedee::bar;
using Embedee::baz;
:
};

This saves having to write and maintain
forwarding functions for these methods.
Of course you can't have more than one
Embedee in this case.
I think one of Herb sutter's "Exceptional
C++" books covers this btw.

HTH

Sep 28 '07 #4
On Sep 28, 7:30 am, puzzlecracker <ironsel2...@gmail.comwrote:
The statement is taken from FAQ [24.2]. What about non-virtual
functions? Can they be overriden? I still don't see a good
justification to prefer private inheritance over composition. In
fact, I have never seen it in a commercial code. If someone did,
please share the use-case and decisions behind it.

Thanks
Private inheritance provides a way to give selective polymorphism
e.g. if a class only wants expose his interface to specific
clients, not necessarily because no true isA relationship exists,
but because selective clients may call the methods e.g.

I provide serviceA, ..B, and ..C. I don't want clientB to make
use of serviceA, but I nevertheless want to provide it to whom
I dictate (I've seen an article on this but can't remember where).

One could achieve the same by other means, but private inheritance
makes it easier. We have an example where we have two parallel
hierarchies (3 deep in this case, each level abstracting a
certain responsibility). The bases in the hierarchy make use of
template method pattern to dictate behavior in terms of
derived classes, and they in turn have enough information to
extend the template method (so to speak). Each of the
classes in the hierarchy make use of services provided by
the same class at different levels, and private inheritance
makes this possible. I have contemplated other ways to do
this, but decided that it is more natural to implement using
private inheritance. Of course, template method itself could
have been implemented using a <bridge>, if you know what I
mean? In the above mentioned case though, things played
out nicely using private inheritance and intent was very clear
(IMHO).

Regards,

Werner

Sep 28 '07 #5
On Sep 28, 7:30 am, puzzlecracker <ironsel2...@gmail.comwrote:
The statement is taken from FAQ [24.2]. What about non-virtual
functions? Can they be overriden? I still don't see a good
justification to prefer private inheritance over composition. In
fact, I have never seen it in a commercial code. If someone did,
please share the use-case and decisions behind it.

Thanks
Private inheritance provides a way to give selective polymorphism
e.g. if a class only wants expose his interface to specific
clients, not necessarily because no true isA relationship exists,
but because selective clients may call the methods e.g.

I provide serviceA, ..B, and ..C. I don't want clientB to make
use of serviceA, but I nevertheless want to provide it to whom
I dictate (I've seen an article on this but can't remember where).

One could achieve the same by other means, but private inheritance
makes it easier. We have an example where we have two parallel
hierarchies (3 deep in this case, each level abstracting a
certain responsibility).

The bases in one hierarchy make use of
template method pattern to dictate behavior in terms of
derived classes, and they in turn have enough information to
extend the template method (so to speak).

Each of the levels in one hierarchy make use of services provided by
corresponding levels in another hierarchy, and private inheritance
makes this possible. I have contemplated other ways to do
this, but decided that it is more natural to implement using
private inheritance. Of course, template method itself could
have been implemented using a <bridge>, if you know what I
mean? In the above mentioned case though, things played
out nicely using private inheritance and intent was very clear
(IMHO).

Regards,

Werner

Sep 28 '07 #6

"werasm" <we****@gmail.comwrote in message
news:11*********************@o80g2000hse.googlegro ups.com...
On Sep 28, 7:30 am, puzzlecracker <ironsel2...@gmail.comwrote:
>The statement is taken from FAQ [24.2]. What about non-virtual
functions? Can they be overriden? I still don't see a good
justification to prefer private inheritance over composition. In
fact, I have never seen it in a commercial code. If someone did,
please share the use-case and decisions behind it.

Thanks

Private inheritance provides a way to give selective polymorphism
e.g. if a class only wants expose his interface to specific
clients, not necessarily because no true isA relationship exists,
but because selective clients may call the methods e.g.

I provide serviceA, ..B, and ..C. I don't want clientB to make
use of serviceA, but I nevertheless want to provide it to whom
I dictate (I've seen an article on this but can't remember where).

One could achieve the same by other means, but private inheritance
makes it easier. We have an example where we have two parallel
hierarchies (3 deep in this case, each level abstracting a
certain responsibility). The bases in the hierarchy make use of
template method pattern to dictate behavior in terms of
derived classes, and they in turn have enough information to
extend the template method (so to speak). Each of the
classes in the hierarchy make use of services provided by
the same class at different levels, and private inheritance
makes this possible. I have contemplated other ways to do
this, but decided that it is more natural to implement using
private inheritance. Of course, template method itself could
have been implemented using a <bridge>, if you know what I
mean? In the above mentioned case though, things played
out nicely using private inheritance and intent was very clear
(IMHO).
I find the comments above very confusing. (Does anyone else?)

How does private inheritance allow a class to expose its interface only to
specific clients? What does "the same class at different levels" mean?
What's a "<bridge>"?

Perhaps some code example might illustrate your point(s) better. I'm just
not following.

-Howard
Sep 28 '07 #7
On Sep 29, 1:46 am, "Howard" <m...@here.comwrote:
I find the comments above very confusing. (Does anyone else?)

How does private inheritance allow a class to expose its interface only to
specific clients?
See the example. Cmd exposes the function execute only to Processor.
What does "the same class at different levels" mean?
What's a "<bridge>"?
Refer to bridge pattern in wikipedia.
Perhaps some code example might illustrate your point(s) better. I'm just
not following.
Here goes:

struct Executable
{
virtual void execute() = 0;
protected:
virtual ~Executable(){ }
};

struct Processor
{
virtual void process( Executable& );
//...
};

class Cmd : Executable
{
public:
Cmd( Processor* p = 0 ): p_( p ){ }
void operator()()
{
if( p_ )
{
p_->process( *this );
}
else
{
execute();
}
}
private:
virtual void execute()
{
//...Some default behavior...
// Most probably overridden
}
Processor* p_;
};
void clientFunction()
{
Processor p;
Cmd cmd( &p );
cmd(); // Processor calls execute...
cmd.execute();//Fails to compile...
}

Regards,

Werner

Sep 29 '07 #8
On Sep 29, 10:32 am, werasm <wer...@gmail.comwrote:
Here goes:

struct Executable
{
virtual void execute() = 0;
protected:
virtual ~Executable(){ }

};

struct Processor
{
virtual void process( Executable& );
//...

};

class Cmd : Executable
{
public:
Cmd( Processor* p = 0 ): p_( p ){ }
void operator()()
{
if( p_ )
{
p_->process( *this );
}
else
{
execute();
}
}
private:
virtual void execute()
{
//...Some default behavior...
// Most probably overridden
}
Processor* p_;

};

void clientFunction()
{
Processor p;
Cmd cmd( &p );
cmd(); // Processor calls execute...
cmd.execute();//Fails to compile...
}
The purpose of this BTW, is to allow the command to be executed
in the context (or thread) or processor when associate with it, and
in the context of its own thread if not. It also allows you to
bind (to derived classes of commands) arguments that get sent
to a callback associated with classes derived from commands.

Unfortunately I can't go the whole nine yards with explaining
this, because the more I explain, the more questions would
be asked, but the little example illustrates the point, I
think.

Regards,

Werner
>
Regards,

Werner

Sep 29 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Will | last post by:
Is it possible to have private instance members in a javascript class? function myObject() { var private = 1; this.getPrivate = function() { return private; } this.incrementPrivate =...
3
by: quo | last post by:
two questions: 1) Does this program demonstrate the basic difference between public and private access? It appears correct to say that instances of a class cannot directly call a private...
12
by: Manolis | last post by:
Hi, I was wondering if there is any way to make two objects of the same class to be able to access each other's private data, like this: class A { public: void access( const A& a )...
5
by: pmatos | last post by:
Hi all, I have a style question. I've been for long programming in Lisp-like languages and C when I need a low-level language. Now, I'm programming for professional reasons in C++ (which I had...
2
by: Christoph Boget | last post by:
Let's take the following class: class MyClass { private int privateVar; public int PublicVar { get { return privateVar; } } public MyClass() {}
6
by: Sahil Malik [MVP] | last post by:
Public Private Key Pairs - How do they work? ----------------------------------------------- I was looking at a presentation recently in which it was suggested that - User 1 Encrypts a message...
6
by: Ken Varn | last post by:
I have an ASP.NET application that is calling a custom class that is trying to parse all of the members of my Page object using Type.GetMembers(). The problem that I am having is that private...
8
by: ifrst | last post by:
What's the defrrence between these(sub,procedure,function,private) and when i use them Als i know it can be function ,sub or procedure Public or private so what does it mean Private only such as...
2
by: Sky | last post by:
Hello: I'm trying to make sense of snk files, when to use, under what conditions to regenerate new ones,...can someone take a look if these statemes make sense? And then the final questions at the...
13
by: PromisedOyster | last post by:
Many in our development team have came from a C++ background and are in the practice of prefixing private class variables with an underscore to improve readability and avoid naming collisions with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.