64 questions
1
vote
0
answers
101
views
How to convert between children types when inheriting privately
Consider this piece of C++ code:
template <typename T>
struct Base
{
// data members ...
// conversion constructors:
template <typename U>
Base(const Base<U>& ...
31
votes
1
answer
1k
views
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 ...
3
votes
2
answers
206
views
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 &...
0
votes
2
answers
504
views
Why does C++ allow this pointer polymorphism of private parent?
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 ...
0
votes
0
answers
50
views
Can I pull all members of a private base into the public scope of an inheriting class?
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 ...
0
votes
0
answers
71
views
Bringing privately inherited Base`s class constructor to the Derived's public scope
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 ...
2
votes
1
answer
100
views
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
{
...
0
votes
1
answer
64
views
C++ How can you Point to a Private Inherited Class Object Within a Class
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 ...
0
votes
1
answer
61
views
upcasting to interface with private inheritance
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 ...
0
votes
1
answer
120
views
Overriding a publically aliased method via private inheritance
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 ...
2
votes
0
answers
29
views
Ambiguous access of unaccessible virtual function [duplicate]
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 ...
0
votes
1
answer
60
views
Can I omit non-public inheritance for forward-declaration of classes?
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 : ...
32
votes
2
answers
2k
views
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 ...
3
votes
2
answers
933
views
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 ...
4
votes
1
answer
834
views
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 ...
-2
votes
3
answers
108
views
How do I access privately inherited class members in C++?
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 ...
4
votes
1
answer
907
views
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 ...
2
votes
3
answers
288
views
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<type*>(e)
dynamic_cast<type&>(e)
dynamic_cast<type&&>(...
4
votes
2
answers
248
views
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 ...
0
votes
4
answers
409
views
Private inheritance usage as described in "The C++ Programming Language"
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 ...
1
vote
2
answers
476
views
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 ...
7
votes
4
answers
3k
views
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<iostream>
using namespace std;
class A
{
public:
A() {cout << "1";}
A(const A &obj) {cout << "2";}
};
class B: virtual A
{
public:
...
8
votes
1
answer
349
views
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 ...
5
votes
1
answer
624
views
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 ...
26
votes
4
answers
5k
views
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 ...
25
votes
5
answers
4k
views
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 ...
3
votes
1
answer
163
views
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 ...
-3
votes
2
answers
63
views
Constructor is available outside of privately inheriting type?
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 &...
0
votes
2
answers
676
views
How to cast to privately derived child type?
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 ...
26
votes
2
answers
1k
views
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, ...
1
vote
1
answer
111
views
why name publicizing is there when we want to inherit the base class privately?
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 ...
6
votes
1
answer
828
views
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 ...
0
votes
1
answer
194
views
How best to expose private inheritance to base class?
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 ...
-1
votes
3
answers
100
views
Private inheritance and returning references from functions
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()
{
...
2
votes
1
answer
213
views
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 ...
7
votes
2
answers
791
views
'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 <iostream>
struct S1 {
void f()...
7
votes
3
answers
829
views
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 ...
2
votes
2
answers
658
views
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 <iostream>
#include <memory>
using namespace std;
struct Test ...
2
votes
1
answer
279
views
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 ...
14
votes
1
answer
292
views
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,...
2
votes
2
answers
114
views
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 ...
1
vote
1
answer
118
views
typecast operator in private base
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 ...
1
vote
1
answer
442
views
Object slicing in private inheritance
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 ...
25
votes
4
answers
6k
views
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 ...
1
vote
1
answer
51
views
need design help using private inheritance
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 ...
7
votes
2
answers
362
views
template private inheritance in vc++10 is not accessible
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 { ...
1
vote
2
answers
128
views
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:
...
6
votes
3
answers
308
views
C++ compiler error involving private inheritance
Could someone please explain the following compiler error to me:
struct B
{
};
template <typename T>
struct A : private T
{
};
struct C : public A<B>
{ ...
0
votes
4
answers
188
views
Why does private inheritance increase the probability, as compared to composition, that someone will break my code?
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 ...
3
votes
1
answer
2k
views
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, ...