473,394 Members | 1,946 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,394 software developers and data experts.

When to use private inheritance

When to do like this:

class A { ... };
class B : private A { ... };

Thanks.

Jan 13 '06 #1
3 3959
vineoff wrote:
When to do like this:

class A { ... };
class B : private A { ... };

Thanks.


If the relationship between A and B is "B has an A" or
B is implemented in terms of A
AND
A has virtual functions (pure or not) that must be
overriden (implemented) in B.

Without the 2nd part it is usually a better choice
if B has a member A and wraps to it.

class B {
public:
void foo()
{
a_.foo();
}
private:
A a_;
};

Regards, Stephan
bro...@osb-systems.com
Open source rating and billing engine for communication networks.

Jan 13 '06 #2
vineoff wrote:
When to do like this:

class A { ... };
class B : private A { ... };


Infrequently. Unlike public inheritance, which indicates an "Is-a"
relationship, private inheritance indicates a "Is-Implemented-With"
relationship. But you can usually use composition instead of private
inheritance to accomplish that:

class B {
A a_;
public:
B();
};

There are always exceptions, of course. If A has virtual functions
that need to be overridden, some would advise having B inherit
privately from A and override those virtual functions. Others would
advise creating a new class, C, that inherits publicly from A and
overrides A's virtual functions, then having class B use composition to
include C:

class A {
virtual void func();
public:
~A();
};

class C : public A {
void func();
};

class B {
C c_;
};

Best regards,

Tom

Jan 13 '06 #3
vineoff wrote:
When to do like this:

class A { ... };
class B : private A { ... };

Thanks.


Also, there are cases in the Standard Library where classes are designed
to be privately inherited.

Case in point: std::priority_queue

The container embedded in the priority_queue is declared protected, but
the priority_queue itself does not have a virtual destructor, so it is
clearly not intended for public inheritance. However, you can privately
inherit, if you need access to the underlying container.
Jan 13 '06 #4

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

Similar topics

4
by: Dave Theese | last post by:
Hello all, The example below demonstrates proper conformance to the C++ standard. However, I'm having a hard time getting my brain around which language rules make this proper... The error...
5
by: Christian Meier | last post by:
Hi dear programmers I looked for the difference between private and protected inheritance, but couldn't find anything. Here is my sample code: #include <iostream> using std::cout; using...
10
by: Ioannis Vranos | last post by:
May someone explain why does this compile? class HiddenSealBaseClass { public: HiddenSealBaseClass() { } }; class Sealed: virtual HiddenSealBaseClass
3
by: bob | last post by:
is this true of private inheritance? public stuff becomes private private stuff becomes inaccessible to the new class protected stays protected and is accessible to the new class
2
by: al pacino | last post by:
hi, is there any advantage of using private inheritance over composition and vice versa. under what condition/s should we chose one over the other. thanks in advance.
31
by: damacy | last post by:
hi, there. i have a problem writing a program which can obtain ip addresses of machines running in the same local network. say, there are 4 machines present in the network; , , and and if i...
6
by: karthikbalaguru | last post by:
Hi, Could someone here tell me some links/pdfs/tutorials to know about the difference between Private Inheritance and Public Inheritance ? I am unable to get info w.r.t it. Thx in advans,...
8
by: puzzlecracker | last post by:
The statement is taken from FAQ . 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...
13
by: PragueExpat | last post by:
I (think) that I've come up with a pattern that I haven't seen in any publications so far and I would like some feedback. Basically, I was looking for a way to inherit private functions and I came...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
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,...
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...

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.