473,545 Members | 2,639 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2027
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...@gm ail.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...@gm ail.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_ex pose_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 "Exceptiona l
C++" books covers this btw.

HTH

Sep 28 '07 #4
On Sep 28, 7:30 am, puzzlecracker <ironsel2...@gm ail.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...@gm ail.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.c omwrote in message
news:11******** *************@o 80g2000hse.goog legroups.com...
On Sep 28, 7:30 am, puzzlecracker <ironsel2...@gm ail.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
justificatio n 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.comw rote:
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.c omwrote:
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
2638
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 = function() {
3
2307
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 method, but a public method can be called by the instance to invoke the private method. 2) So is it true that only public methods of a class can invoke...
12
2657
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 ) {cout<<"a.value="<<a.value<<endl; } private: int value;
5
2168
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 programmed in before but only in college) and I'm getting to like it, however I'm having some issues with style. For example, If I have an object...
2
1817
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
2540
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 using User 2's Public Key. User 2 Decrypts the transmission using his Private Key to get the orignal message. Is the above correct?
6
4779
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 members are not returned. I did some digging and the MSDN documentation states that the caller must have ReflectionPermission in order to get the...
8
1474
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 Private ask( ....... statmen ........ End private
2
2430
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 end that they first statements bring up in my mind... a) Because two developers, unbeknownst to each other, can end up releaseing different...
13
3058
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 local variables and parameters. This has become our standard. ie private string _name; as opposed to: ie private string name;
0
7490
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7682
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7935
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7449
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7780
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6009
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3479
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3465
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.