Questions tagged [private-inheritance]

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.

63 questions
32
votes
1 answer

Passing a pointer to a type that is privately inherited in some base class

I always assumed that private inheritance simply means that a type doesn't tell the outside that it's inheriting from some base class. However, it seems that there are more restrictions. Consider the following minimal example: struct MyInterface…
Brotcrunsher
  • 1,964
  • 10
  • 32
32
votes
2 answers

Trivial cases of shared_ptr and weak_ptr failing

I'm having trouble using shared_ptr and weak_ptr along with enable_shared_from_this. When I google the symptoms of what I'm seeing, everybody suggests "you cannot use shared_from_this() when there are no shared_ptr instances owning your object. But…
26
votes
2 answers

A weird behavior of using-declaration

please see the following code struct A { using type = int; }; struct B : private A {}; struct C : B { using base_type = A; }; All of gcc 6.1, clang 3.8, and msvc 2015 update 3 refuse to compile this, as A is not an accessible name inside C since A…
Junekey Jeon
  • 1,496
  • 1
  • 11
  • 18
25
votes
5 answers

How to call a static method from a private base class?

Due to the layout of a third-party library, I have something like the following code: struct Base { static void SomeStaticMethod(){} }; struct Derived1: private Base {}; struct Derived2: public Derived1 { void SomeInstanceMethod(){ …
Carlton
  • 4,217
  • 2
  • 24
  • 40
24
votes
3 answers

Can I cast a derived class to a private base class, using C-style cast?

Can I do this? class A { ... }; class B : private A { const A &foo() const { return *((const A *)this); } }; Can I take a subclass that inherits privately from a base class and cast it to a public version of its base class? Can…
AdamIerymenko
  • 1,299
  • 2
  • 10
  • 19
23
votes
4 answers

std::enable_shared_from_this; public vs private

I was playing around for a bit using the shared_ptr's and enable_shared_from_this, while I run into something I don't really understand. In my first attempt I constructed something like this: class shared_test :…
19
votes
3 answers

When to use C++ private inheritance over composition?

Can you give me a concrete example when is preferable to use private inheritance over composition? Personally, I will use composition over private inheritance, but there might be the case that using private inheritance is the best solution for a…
19
votes
3 answers

C++ private virtual inheritance problem

In the following code, it seems class C does not have access to A's constructor, which is required because of the virtual inheritance. Yet, the code still compiles and runs. Why does it work? class A {}; class B: private virtual A {}; class C:…
Oak
  • 26,231
  • 8
  • 93
  • 152
14
votes
1 answer

Why auto_ptr seems to breach private inheritance on Visual C++?

Background information: This was detected on Visual Studio 2008, and confirmed again on Visual Studio 2013. G++ screamed at the code, while Visual accepted the private inheritance breach silently. So, on Visual C++, we have the following code: class…
paercebal
  • 81,378
  • 38
  • 130
  • 159
13
votes
1 answer

Private inheritance: name lookup error

I have the following code example that doesn't compile: #include namespace my { class base1 { // line 6 }; class base2: private base1 { }; class derived: private base2 { public: // The…
anatolyg
  • 26,506
  • 9
  • 60
  • 134
12
votes
2 answers

C++ Exceptions and Inheritance from std::exception

Given this sample code: #include #include class my_exception_t : std::exception { public: explicit my_exception_t() { } virtual const char* what() const throw() { return "Hello, world!"; } }; int main() { …
fbrereto
  • 35,429
  • 19
  • 126
  • 178
11
votes
3 answers

Pointer to a member function in an inaccessible base

The compilation of the next example : class A { public: void foo() { } }; class B : private A { public: using A::foo; }; int main() { typedef void (B::*mf)(); mf func = &B::foo; B b; (b.*func)(); } fails…
BЈовић
  • 62,405
  • 41
  • 173
  • 273
8
votes
1 answer

Why does Visual Studio compiler allow violation of private inheritance in this example?

I found very strange behavior of std::unique_ptr in Visual Studio 2013 and 2017. Let's consider an example: class Base { public: virtual ~Base() = default; virtual void Foo() = 0; }; class Derived : private Base { public: void Foo()…
Viktor
  • 1,004
  • 1
  • 10
  • 16
7
votes
4 answers

How to call copy constructor of all base classes for copying most derived class object in diamond inheritance in C++?

Consider the below code: #include using namespace std; class A { public: A() {cout << "1";} A(const A &obj) {cout << "2";} }; class B: virtual A { public: B() {cout << "3";} B(const B & obj) {cout<< "4";} }; class C:…
7
votes
3 answers

Erroneous private base class inaccessible?

Compiling this code using g++ 4.2.1: struct S { }; template struct ST { }; template class ref_count : private BaseType { }; template class rep_base : public RefCountType { }; class wrap_rep :…
Paul J. Lucas
  • 6,895
  • 6
  • 44
  • 88
1
2 3 4 5