Skip to main content
Filter by
Sorted by
Tagged with
1 vote
0 answers
101 views

Consider this piece of C++ code: template <typename T> struct Base { // data members ... // conversion constructors: template <typename U> Base(const Base<U>& ...
lobelk's user avatar
  • 531
31 votes
1 answer
1k views

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 ...
Brotcrunsher's user avatar
  • 2,304
3 votes
2 answers
206 views

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 &...
Sju Ton's user avatar
  • 45
0 votes
2 answers
504 views

If I inherit a derived class privately from a base class, I cannot get the polymorphism of the inhritetted class. But I can get the polymorphism of 'this' pointer inside the derived class. I'm very ...
sake's user avatar
  • 405
0 votes
0 answers
50 views

When inheriting privately from a base class, I can say public: using Base::member; to make that (non-private) member public within the inheriting class. Is there some way to expand this to get all the ...
bitmask's user avatar
  • 35.2k
0 votes
0 answers
71 views

Inheriting a constructor from a private template class in C++ explaining that by using Base::Base; inside Derived we're bringing the type Base injected in Base as Base::Base to scope of Derived and ...
Soup  Endless's user avatar
2 votes
1 answer
100 views

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 { ...
Nater's user avatar
  • 165
0 votes
1 answer
64 views

I am learning about class inheritance and I wanted to know how one can create a pointer to a class that was inherited privately by another class? I've included a simple example below. Many thanks to ...
vectorjon's user avatar
0 votes
1 answer
61 views

I have the following situation class Interface { }; class ComplicatedObject : public Interface { }; and I would like to give a simplified version of the ComplicatedObject, still providing the ...
Teloze's user avatar
  • 289
0 votes
1 answer
120 views

I have a class heirarchy where there is one base type Base with a list of implementations and another base class, AnotherBase, that is almost like Base but a little different. To express this in the ...
Curious's user avatar
  • 21.3k
2 votes
0 answers
29 views

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 ...
user avatar
0 votes
1 answer
60 views

Let's say I have a piece of code like this: // Foo.h: class Incomplete; // the forward-declaration class Foo { void bar(Incomplete&); // doesn't really matter }; // Foo.cpp: class Incomplete : ...
passing_through's user avatar
32 votes
2 answers
2k views

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 ...
LubosD's user avatar
  • 840
3 votes
2 answers
933 views

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 ...
Tofu's user avatar
  • 3,614
4 votes
1 answer
834 views

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 ...
user1806566's user avatar
  • 1,241
-2 votes
3 answers
108 views

I'm trying to understand the private inheritance concept. So far everywhere I see they write that private inheritance makes its members inaccessible from the derived class. Doesn't this make it sort ...
Anonymous Guy's user avatar
4 votes
1 answer
907 views

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 ...
Jeremy Friesner's user avatar
2 votes
3 answers
288 views

Quoted from C++ Primer 5th 19.2.1. The dynamic_cast Operator A dynamic_cast has the following form: dynamic_cast<type*>(e) dynamic_cast<type&>(e) dynamic_cast<type&&>(...
choxsword's user avatar
  • 3,387
4 votes
2 answers
248 views

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 ...
Chris N's user avatar
  • 53
0 votes
4 answers
409 views

In The C++ Programming Language, 4th Edition, at §20.5.2 "Access to Base Class" (page 605), it says (regarding private inheritance): private bases are most useful when defining a class by ...
yonutix's user avatar
  • 2,511
1 vote
2 answers
476 views

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 ...
Gaurav's user avatar
  • 181
7 votes
4 answers
3k views

Consider the below code: #include<iostream> using namespace std; class A { public: A() {cout << "1";} A(const A &obj) {cout << "2";} }; class B: virtual A { public: ...
Gaurav's user avatar
  • 181
8 votes
1 answer
349 views

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 ...
Viktor's user avatar
  • 1,064
5 votes
1 answer
624 views

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 ...
JBL's user avatar
  • 13k
26 votes
4 answers
5k views

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 ...
user2546926's user avatar
25 votes
5 answers
4k views

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 ...
Carlton's user avatar
  • 4,305
3 votes
1 answer
163 views

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 ...
Flowing Cloud's user avatar
-3 votes
2 answers
63 views

The title is only one of a few things I am confused on about the following example: struct A { A() { std::cout << "Default constructor of A" << std::endl; } A(const A &...
AlwaysLearning's user avatar
0 votes
2 answers
676 views

The following is an attempt at implementing a shared pointer with a modified semantics of operator==: template <typename T> struct deref_shared_ptr: private std::shared_ptr<T> { using ...
AlwaysLearning's user avatar
26 votes
2 answers
1k views

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, ...
Junekey Jeon's user avatar
  • 1,597
1 vote
1 answer
111 views

Generally, we wish to use private inheritance to hide the implementation details to the base class. If this is true, 1) Why is the name publicizing feature is there again ? Is it only for language ...
Bharadwaj Gali's user avatar
6 votes
1 answer
828 views

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 ...
Anders Johansson's user avatar
0 votes
1 answer
194 views

I am designing an object hierarchy in which the base class of all objects is Node, which is expected to be subclassed. Subclasses of Node will contain child items, but the type of child can vary from ...
Carlton's user avatar
  • 4,305
-1 votes
3 answers
100 views

Now I have code below: class Env { public: int ra(){ return a;} int rb(){ return b;} private: int a; int b; }; class CEnv: private Env { public: static Env* Instance() { ...
xingfu0539's user avatar
2 votes
1 answer
213 views

Given the following example: class A { protected: static void useful_function_without_side_effects() {...} } class B : private A { // B has no friends :( public: void ...
Jack Sabbath's user avatar
  • 1,478
7 votes
2 answers
791 views

Could I use SFINAE (or another technique) for using declaration while private deriving from template class? For better understanding see code below: #include <iostream> struct S1 { void f()...
αλεχολυτ's user avatar
7 votes
3 answers
829 views

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 ...
N0vember's user avatar
  • 1,095
2 votes
2 answers
658 views

I have a question. I was playing with enable_shared_from_this and noticed a strange thing. This example works fine: #include <iostream> #include <memory> using namespace std; struct Test ...
Jack's user avatar
  • 21
2 votes
1 answer
279 views

I have the following: class Abstract { virtual void AbstractMethod() = 0; }; class Implementer { void AbstractMethod() {}; }; class Concrete : public Abstract, private Implementer {}; I ...
Dan Nestor's user avatar
  • 2,541
14 votes
1 answer
292 views

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,...
paercebal's user avatar
  • 83.9k
2 votes
2 answers
114 views

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 ...
uknowit's user avatar
  • 33
1 vote
1 answer
118 views

I found something I consider strange behaviour in C++: a typecast operator in a private base class is confusing the compiler when trying to resolve an implicit cast: #include <iostream> struct ...
Matei David's user avatar
  • 2,382
1 vote
1 answer
442 views

Why object slicing does not takes place in private inheritance? Static_cast gives error in such cases? I understand that the private inheritance does not hold “is - a” relationship between its ...
Bhupesh Pant's user avatar
  • 4,409
25 votes
4 answers
6k views

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 ...
AdamIerymenko's user avatar
1 vote
1 answer
51 views

I have a problem with the following situation: a library (CardReader) implements the ISO7816 protocol and communicates with a smart card (implemented by me). I have to implement a proprietary ...
Elvis Dukaj's user avatar
  • 7,428
7 votes
2 answers
362 views

The following code compiles using GCC 4.4.6 and Comeau 4.3.10. #include <iostream> struct A { int name; }; template<typename T> struct C : T { using T::name; }; struct B : private A { ...
Nick's user avatar
  • 5,805
1 vote
2 answers
128 views

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: ...
linuxeasy's user avatar
  • 6,499
6 votes
3 answers
308 views

Could someone please explain the following compiler error to me: struct B { }; template <typename T> struct A : private T { }; struct C : public A<B> { ...
HighCommander4's user avatar
0 votes
4 answers
188 views

The author of this article states that "Normally you don't want to have access to the internals of too many other classes, and private inheritance gives you some of this extra power (and ...
Belloc's user avatar
  • 6,400
3 votes
1 answer
2k views

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, ...
Belloc's user avatar
  • 6,400