| 1 |
// { dg-do assemble } |
| 2 |
// { dg-skip-if "requires hosted libstdc++ for iostream" { ! hostedlib } } |
| 3 |
// GROUPS passed visibility |
| 4 |
// visibility file |
| 5 |
// From: Gordon Joly <[email protected]> |
| 6 |
// Date: Wed, 21 Apr 93 09:42:07 +0100 |
| 7 |
// Subject: /*** BUG REPORT : THE MYTH OF PRIVATE INHERITANCE ***/ |
| 8 |
// Message-ID: <[email protected]> |
| 9 |
#include <iostream> |
| 10 |
|
| 11 |
class A { |
| 12 |
private: |
| 13 |
int number; |
| 14 |
public: |
| 15 |
A(int i) : number(i) |
| 16 |
{} |
| 17 |
virtual ~A() |
| 18 |
{} |
| 19 |
virtual void Number(int c) // { dg-message "declared" } |
| 20 |
{ number = c; } |
| 21 |
virtual int Number() // { dg-message "declared" } |
| 22 |
{ return number; } |
| 23 |
}; |
| 24 |
|
| 25 |
class B : private A { |
| 26 |
private: |
| 27 |
int second_number; |
| 28 |
public: |
| 29 |
B(int c, int i) : second_number(c), A(i) |
| 30 |
{} |
| 31 |
virtual ~B() |
| 32 |
{} |
| 33 |
|
| 34 |
virtual void firstNumber(int b) // renames member function Number(int) of class A |
| 35 |
{ A::Number(b); } |
| 36 |
virtual int firstNumber() // renames member function Number() of class A |
| 37 |
{ return A::Number(); } |
| 38 |
}; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
class C { |
| 44 |
private: |
| 45 |
B* bobject; |
| 46 |
public: |
| 47 |
C(B* bp) : bobject(bp) |
| 48 |
{} |
| 49 |
virtual ~C() |
| 50 |
{} |
| 51 |
// |
| 52 |
// the following two functions access |
| 53 |
// private member functions of class B |
| 54 |
// and they should not be able to do so |
| 55 |
// |
| 56 |
virtual void setBValue(int i) |
| 57 |
{ if (bobject) bobject->Number(i); } // { dg-error "this context|accessible base" } |
| 58 |
virtual int getBValue() |
| 59 |
{ if (bobject) { return bobject->Number(); } return 0; } // { dg-error "this context|accessible base" } |
| 60 |
}; |
| 61 |
|
| 62 |
|
| 63 |
int main() |
| 64 |
{ |
| 65 |
B* bobject = new B(2, 1); |
| 66 |
C* cobject = new C(bobject); |
| 67 |
cobject->setBValue(8); |
| 68 |
std::cout << cobject->getBValue() << std::endl; |
| 69 |
delete bobject; |
| 70 |
delete cobject; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|