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
3
votes
2 answers
Why does enable_shared_from_this crash if inheritance is not public instead of erroring out
I was using shared_ptr in a project. And at one point I had to store the raw pointers as a void then later convert it back to its shared_ptr form in a callback where the void* was passed. But for some reason the code kept crashing. I didn't…

Irelia
- 3,407
- 2
- 10
- 31
3
votes
1 answer
Difference among three explicit upcasting to a private base class
I have the following three typecastings between the private inherited base class object and the child object, two of them work, but the last one doesn't. I am wondering what causes the different results.
#include
#include
using…

Flowing Cloud
- 433
- 1
- 4
- 8
2
votes
1 answer
The direct base class of private inheritance prevents the derived class from defining the object of the indirect base class
like this code
class Try
{
public:
Try() = default;
int i = 0;
};
class B1 : private Try
{
public:
B1() = default;
using Try::Try();
using Try::i;
};
class C1 : public B1
{
public:
Try a; //tell me:'Try' is a private member…

Nater
- 165
- 7
2
votes
0 answers
Ambiguous access of unaccessible virtual function
Consider this code:
struct A
{
virtual void foo() {};
};
struct B : private A {};
struct C : private A {};
struct D : A, B, C {};
int main()
{
D d;
d.foo(); // Error: ambiguous access of foo (is it A::foo(), B::foo() or C::foo()…

user7769147
- 1,559
- 9
- 15
2
votes
3 answers
Is the c++ primer making something wrong with the usage of `dynamic_cast`?
Quoted from C++ Primer 5th 19.2.1. The dynamic_cast Operator
A dynamic_cast has the following form:
dynamic_cast(e)
dynamic_cast(e)
dynamic_cast(e)
where type must be a class type and (ordinarily) names a class that has…

choxsword
- 3,187
- 18
- 44
2
votes
1 answer
C++ can (virtual) private base classes be removed by the compiler?
Given the following example:
class A
{
protected:
static void useful_function_without_side_effects() {...}
}
class B : private A
{
// B has no friends :(
public:
void travel_back_in_time() { super_useful_function();…

Jack Sabbath
- 1,398
- 2
- 9
- 12
2
votes
2 answers
Class vs struct with enable_shared_from_this
I have a question. I was playing with enable_shared_from_this and noticed a strange thing. This example works fine:
#include
#include
using namespace std;
struct Test : enable_shared_from_this
{
};
int main() {
…

Jack
- 21
- 1
2
votes
1 answer
How can I override a pure virtual method using a privately inherited method?
I have the following:
class Abstract
{
virtual void AbstractMethod() = 0;
};
class Implementer
{
void AbstractMethod() {};
};
class Concrete : public Abstract, private Implementer
{};
I cannot instantiate Concrete because the pure virtual…

Dan Nestor
- 2,441
- 1
- 24
- 45
2
votes
2 answers
Object Instantiations couting using composition in c++
In More effective C++ Meyers has described a way to count the instantiation of the objects using an object counting base class (item 26). Is it possible to implement the same using composition technique as below . Is there a specific advantage using…

uknowit
- 33
- 5
1
vote
2 answers
How can a parent members be called in Private Inheritance?
I am going through a book on Design Patterns by GoF - online link.
In this book, in Adapter pattern, in Sample Code section, I have come across this particular code:
class TextView {
public:
TextView();
void GetOrigin(Coord&…

linuxeasy
- 6,269
- 7
- 33
- 40
1
vote
2 answers
is it possible to hide an overloaded method while using private inheritence in c++
class Foo
{
public:
int fn()
{
return 1;
}
int fn(int i)
{
return i; //2nd fn()
}
};
class Bar:Foo
{
public :
Foo::fn;
};
int main(int argc, char** argv)
{
Bar b;
cout<

Sleiman Jneidi
- 22,907
- 14
- 56
- 77
1
vote
3 answers
Private Inheritance: How do I make object of the Base Class ( which has got pure virtual methods)?
Consider the following code:
class Base
{
protected:
virtual void methodDefinedInBase() = 0;
}
Class Derived: private Base
{
public:
void someMethod();
protected:
virtual void methodDefinedInBase()
{
std::cout<<"From…

Vishnu Pedireddi
- 2,142
- 4
- 25
- 34
1
vote
5 answers
Simultaneous private and public inheritance in C++
Suppose a class Y publicly inherits a class X. Is it possible for a class Z to privately inherit Y while publicly inheriting X?
To make this clearer, suppose X defines public methods x1 and x2. Y inherits X, overrides x1 and provides a method y.…

gspr
- 11,144
- 3
- 41
- 74
1
vote
2 answers
Why default constructor of most base class (Virtual) is not getting called in private virtual inheritance while creating object of most derived class?
How default constructor of most base class is getting called in private virtual inheritance while creating object of most derived class. But the same does not get called when mentioned in constructor initializer list of most derived…

Gaurav
- 161
- 1
- 11
1
vote
2 answers
Private inheritance and swap
I'm using private inheritance in the implementation of two very related classes. The using Base::X; is very useful and elegant. However, I can't seem to find an elegant solution for reusing the base class's swap function.
class A
{
public:
…

deft_code
- 57,255
- 29
- 141
- 224