private-inheritance

C++ compiler error involving private inheritance

孤街醉人 提交于 2020-01-03 10:56:11
问题 Could someone please explain the following compiler error to me: struct B { }; template <typename T> struct A : private T { }; struct C : public A<B> { C(A<B>); // ERROR HERE }; The error at the indicated line is: test.cpp:2:1: error: 'struct B B::B' is inaccessible test.cpp:12:7: error: within this context What exactly is inaccessible, and why? 回答1: Try A< ::B> or A<struct B> . Inside of C , unqualified references to B will pick up the so-called injected-class-name , it is brought in through

Function member pointer with private base

北战南征 提交于 2019-12-22 07:04:10
问题 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 <iostream> using namespace std; class base { public: int i; void print(int i) { printf("base i\n"); } }; class base_der : private base { public: using base::print; }; int main() { // This works: base_der cls; cls.print(10); // This doesn't: void (base_der::* print)(int); print

C++ private virtual inheritance problem

℡╲_俬逩灬. 提交于 2019-12-22 01:26:48
问题 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: public B {}; int main() { C c; return 0; } Moreover, if I remove the default constructor from A, e.g. class A { public: A(int) {} }; class B: private virtual A { public: B() : A(3) {} }; then class C: public B {}; would (unexpectedly) compile, but class

std::enable_shared_from_this; public vs private

前提是你 提交于 2019-12-18 07:33:43
问题 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 : std::enable_shared_from_this<shared_test> { public: void print(bool recursive) { if (recursive) { shared_from_this()->print(false); } std::cout << "printing" << std::endl; } }; Please note that this class is extending std::enable_shared_from_this privately. This apparently makes a lot

std::enable_shared_from_this; public vs private

眉间皱痕 提交于 2019-12-18 07:33:20
问题 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 : std::enable_shared_from_this<shared_test> { public: void print(bool recursive) { if (recursive) { shared_from_this()->print(false); } std::cout << "printing" << std::endl; } }; Please note that this class is extending std::enable_shared_from_this privately. This apparently makes a lot

Is the c++ primer making something wrong with the usage of `dynamic_cast`?

喜你入骨 提交于 2019-12-12 17:27:06
问题 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&&>(e) where type must be a class type and (ordinarily) names a class that has virtual functions. In the first case, e must be a valid pointer (§ 2.3.2, p. 52); in the second, e must be an lvalue; and in the third, e must not be an lvalue. In all cases, the type of e must be either a class type that is publicly derived from the

template private inheritance in vc++10 is not accessible

拈花ヽ惹草 提交于 2019-12-08 17:41:59
问题 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 { friend struct C<B>; }; int main() { C<B> o; o.name = 0; } It gives the following error in VC++10: main.cpp(4): error C2877: 'A::name' is not accessible from 'A' main.cpp(10): error C2247: 'A::name' not accessible because 'B' uses 'private' to inherit from 'A' What's a good cross-compiler workaround that allows o

How to cast to privately derived child type?

[亡魂溺海] 提交于 2019-12-08 12:11:09
问题 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 Base = std::shared_ptr<T>; // ... using statements to include functionality from the base. bool operator==(const deref_shared_ptr rhs) const { return (**this == *rhs); } }; I am struggling with implementing an equivalent of std::make_shared for this type. This is my attempt: template< class T, class... Args >

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

狂风中的少年 提交于 2019-12-07 03:54:08
问题 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: B() {cout << "3";} B(const B & obj) {cout<< "4";} }; class C: virtual A { public: C() {cout << "5";} C(const C & obj) {cout << "6";} }; class D:B,C { public: D() {cout << "7";} D(const D & obj) {cout << "8";} }; int main() { D d1; cout << "\n"; D d(d1); } The output of the program is below: 1357 1358 So, for line D d(d1) the copy

Function member pointer with private base

别来无恙 提交于 2019-12-05 08:25:44
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 <iostream> using namespace std; class base { public: int i; void print(int i) { printf("base i\n"); } }; class base_der : private base { public: using base::print; }; int main() { // This works: base_der cls; cls.print(10); // This doesn't: void (base_der::* print)(int); print = &base_der::print; // Compile error here } I think there are a few interacting problems contributing