Private inheritance is a form of inheritance is which the public and protected portion of the base class becomes private in the derived class and the derived class has no access to the private members and methods of the base class.
Questions tagged [private-inheritance]
63 questions
7
votes
2 answers
'using' declaration as SFINAE
Could I use SFINAE (or another technique) for using declaration while private deriving from template class?
For better understanding see code below:
#include
struct S1 {
void f() { std::cout << "S1::f\n"; }
};
struct S2 {
void…

αλεχολυτ
- 4,792
- 1
- 35
- 71
7
votes
3 answers
Why doesn't private inheritance resolve ambiguity for static functions ? (tested in MSVC)
I'm wondering why a call to a static function is ambiguous, even when one of the two is obviously impossible to call as it is private.
I was hoping I could use private / protected inheritance to help the compiler solve the ambiguity.
Is it specific…

N0vember
- 995
- 1
- 9
- 12
7
votes
2 answers
template private inheritance in vc++10 is not accessible
The following code compiles using GCC 4.4.6 and Comeau 4.3.10.
#include
struct A { int name; };
template struct C : T { using T::name; };
struct B : private A { friend struct C; };
int main()
{
C o;
o.name =…

Nick
- 5,765
- 5
- 27
- 36
6
votes
3 answers
C++ compiler error involving private inheritance
Could someone please explain the following compiler error to me:
struct B
{
};
template
struct A : private T
{
};
struct C : public A
{
…

HighCommander4
- 50,428
- 24
- 122
- 194
6
votes
3 answers
Function member pointer with private base
The following code yields a compile time error:
'base::print' : cannot access private member declared in class 'base_der'
However, I have made the member public in the derived class. Why doesn't this work?
#include
using namespace…

SerbanLupu
- 427
- 2
- 5
- 9
6
votes
1 answer
Binding to privately inherited member function
I'd like to std::bind to a member function from a private base class, made "public" with a using-declaration in the derived class. Calling the function directly works, but it seems binding or using member function pointers doesn't compile:
#include…

Anders Johansson
- 3,926
- 19
- 19
5
votes
1 answer
Override public virtual function with private base function?
Let's consider two classes A and B with the following interface:
class A {
public:
virtual void start() {} //default implementation does nothing
};
class B {
public:
void start() {/*do some stuff*/}
};
and then a third class which inherits…

JBL
- 12,588
- 4
- 53
- 84
4
votes
2 answers
How to use Private Inheritance aka C++ in C# and Why not it is present in C#
I know that private inheritance is supported in C++ and only public inheritance is supported in C#. I also came across an article which says that private inheritance usually defines a HAS-A relationship and kind of an aggregation relationship…

S2S2
- 8,322
- 5
- 37
- 65
4
votes
5 answers
does it make sense to inherit privately from an abstract (pure virtual) class?
suppose this construct
struct InterfaceForFoo
{
virtual void GetItDone() = 0;
};
class APoliticallyCorrectImplementationOfFooRelatedThings : private InterfaceForFoo
{
public:
void GetItDone() { /*do the thing already*/ };
};
Now,…

lurscher
- 25,930
- 29
- 122
- 185
4
votes
1 answer
In C++, is it possible to use CRTP with a private base?
In C++ I have many classes, unrelated by inheritance, that define a method std::string get_name() const.
There are several utility functions that many classes need that are implemented in terms of get_name(). I would like classes that implement…

user1806566
- 1,078
- 6
- 18
4
votes
0 answers
How to exclude a class from the DOxygen inheritance graphs?
I've got a templated C++ class in my project that gets used only for debugging purposes -- in normal builds it compiles down to an empty/no-op class. This class gets privately-inherited by many of my other classes.
That works fine for its purpose;…

Jeremy Friesner
- 70,199
- 15
- 131
- 234
4
votes
2 answers
Private inheritance, return reference to static member of base class
I have a simple question regarding inheriting from a class which privately inheriting of a base class, i.e. we have
class Base {};
class Heir: private Base {};
class HeirsHeir : public Heir {};
In understand that HeirsHeir cannot access anything of…

Chris N
- 53
- 7
3
votes
1 answer
Private inheritance vs containment
While explaining when private inheritance must be used, instead of containment, the author of this article says the following :
"We need to construct the used object before, or destroy it after, another base subobject. If the slightly longer object…

Belloc
- 6,318
- 3
- 22
- 52
3
votes
4 answers
What is private inheritance, and what issues(s) does it address?
Could someone explain what exactly private/protected inheritance in C++ is for, and what problem(s) it is intended to solve?
class Bar { };
class Foo : private Bar { };
I've already seen this question but I still don't understand what it is, let…

user541686
- 205,094
- 128
- 528
- 886
3
votes
2 answers
About private inheritance in C++
Why cannot I privately derive a class from both a class and another class that the class being derived from uses as a base. So, I have the the following
class Money
{
public:
void get() {std::cin >> amount;}
private:
float amount;
}
class…

Sju Ton
- 45
- 4