private-inheritance

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

我的梦境 提交于 2019-12-05 07:49:00
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 constructor of D class is bein called. During inheritance we need to explicitly call copy constructor

Erroneous private base class inaccessible?

拜拜、爱过 提交于 2019-12-04 23:24:26
问题 Compiling this code using g++ 4.2.1: struct S { }; template<typename T> struct ST { }; template<typename BaseType> class ref_count : private BaseType { }; template<typename RefCountType> class rep_base : public RefCountType { }; class wrap_rep : public rep_base<ref_count<S> > { typedef rep_base<ref_count<S> > base_type; // line 11 }; I get: bug.cpp:1: error: ‘struct S’ is inaccessible bug.cpp:11: error: within this context However, if I change the wrap_rep class to use ST : class wrap_rep :

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

半世苍凉 提交于 2019-12-03 14:44:45
问题 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(){ Base::SomeStaticMethod(); } }; int main() { Derived2 d2; d2.SomeInstanceMethod(); return 0; } I'm getting compiler error C2247 with MSVC: Base::SomeStaticMethod not accessible because Derived1 uses private to inherit from Base. I know I can't access Base members

Erroneous private base class inaccessible?

无人久伴 提交于 2019-12-03 13:18:05
Compiling this code using g++ 4.2.1: struct S { }; template<typename T> struct ST { }; template<typename BaseType> class ref_count : private BaseType { }; template<typename RefCountType> class rep_base : public RefCountType { }; class wrap_rep : public rep_base<ref_count<S> > { typedef rep_base<ref_count<S> > base_type; // line 11 }; I get: bug.cpp:1: error: ‘struct S’ is inaccessible bug.cpp:11: error: within this context However, if I change the wrap_rep class to use ST : class wrap_rep : public rep_base<ref_count< ST<int> > > { typedef rep_base<ref_count< ST<int> > > base_type; }; it

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

故事扮演 提交于 2019-12-03 04:29:40
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(){ Base::SomeStaticMethod(); } }; int main() { Derived2 d2; d2.SomeInstanceMethod(); return 0; } I'm getting compiler error C2247 with MSVC: Base::SomeStaticMethod not accessible because Derived1 uses private to inherit from Base. I know I can't access Base members from Derived2 via inheritance because of the private specifier, but I should still be able to call a

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

偶尔善良 提交于 2019-12-01 15:08:21
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 Base {}; class Derived : Base {}; // inherits privately. Adding explicitly the // keyword private changes nothing int main() { std::auto_ptr<Base>(new Derived) ; // compiles, which is NOT EXPECTED std::auto_ptr<Base> p(new Derived) ; // Does not compile, which is expected } Why would the first (temporary) auto_ptr compile? I went inside it in debug

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

半腔热情 提交于 2019-12-01 13:59:01
问题 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 Base {}; class Derived : Base {}; // inherits privately. Adding explicitly the // keyword private changes nothing int main() { std::auto_ptr<Base>(new Derived) ; // compiles, which is NOT EXPECTED std::auto_ptr<Base> p(new Derived) ; // Does not

C++ Exceptions and Inheritance from std::exception

泄露秘密 提交于 2019-11-30 17:59:32
Given this sample code: #include <iostream> #include <stdexcept> class my_exception_t : std::exception { public: explicit my_exception_t() { } virtual const char* what() const throw() { return "Hello, world!"; } }; int main() { try { throw my_exception_t(); } catch (const std::exception& error) { std::cerr << "Exception: " << error.what() << std::endl; } catch (...) { std::cerr << "Exception: unknown" << std::endl; } return 0; } I get the following output: Exception: unknown Yet simply making the inheritance of my_exception_t from std::exception public , I get the following output: Exception:

Private inheritance: name lookup error

怎甘沉沦 提交于 2019-11-30 13:56:19
问题 I have the following code example that doesn't compile: #include <stdio.h> namespace my { class base1 { // line 6 }; class base2: private base1 { }; class derived: private base2 { public: // The following function just wants to print a pointer, nothing else! void print(base1* pointer) {printf("%p\n", pointer);} }; } The error that gcc prints is: test.cpp:6: error: `class my::base1' is inaccessible test.cpp:17: error: within this context Now, i can guess what the problem is: when looking at

C++ Exceptions and Inheritance from std::exception

三世轮回 提交于 2019-11-30 01:39:19
问题 Given this sample code: #include <iostream> #include <stdexcept> class my_exception_t : std::exception { public: explicit my_exception_t() { } virtual const char* what() const throw() { return "Hello, world!"; } }; int main() { try { throw my_exception_t(); } catch (const std::exception& error) { std::cerr << "Exception: " << error.what() << std::endl; } catch (...) { std::cerr << "Exception: unknown" << std::endl; } return 0; } I get the following output: Exception: unknown Yet simply making