| 1 |
// { dg-do assemble } |
| 2 |
// { dg-skip-if "requires hosted libstdc++ for iostream" { ! hostedlib } } |
| 3 |
// GROUPS passed visibility |
| 4 |
// visibility file |
| 5 |
// From: [email protected] (Dinh Le) |
| 6 |
// Date: Mon, 12 Jul 93 22:21:06 -0700 |
| 7 |
// Subject: class, template and their scoping problem |
| 8 |
// Message-ID: <[email protected]> |
| 9 |
|
| 10 |
#include <iostream> |
| 11 |
#include <cassert> |
| 12 |
|
| 13 |
// --------------- Array.h && Array.cc ------------------ |
| 14 |
|
| 15 |
using namespace std; |
| 16 |
|
| 17 |
const int ArraySize = 12; |
| 18 |
|
| 19 |
template <class> class Array_RC; |
| 20 |
|
| 21 |
template <class Type> |
| 22 |
class Array { |
| 23 |
friend class Array_RC<Type>; |
| 24 |
public: |
| 25 |
Array(const Type *ar, int sz) { init(ar,sz); } |
| 26 |
virtual ~Array() { delete [] ia; } |
| 27 |
virtual void print(ostream& = cout); |
| 28 |
virtual Type& operator[](int ix) { return ia[ix]; } |
| 29 |
private: |
| 30 |
void init(const Type*, int); |
| 31 |
int size; |
| 32 |
int *ia; |
| 33 |
}; |
| 34 |
|
| 35 |
template <class Type> |
| 36 |
ostream& operator<<( ostream& os, Array<Type>& ar ) |
| 37 |
{ |
| 38 |
ar.print(os); |
| 39 |
return os; |
| 40 |
} |
| 41 |
|
| 42 |
template <class Type> |
| 43 |
void Array<Type>::print(ostream& os) |
| 44 |
{ |
| 45 |
const int lineLength = 12; |
| 46 |
|
| 47 |
os << "( " << size << " )< "; |
| 48 |
for (int ix = 0; ix < size; ++ix) { |
| 49 |
if (ix % lineLength == 0 && ix) os << "\n\t"; |
| 50 |
os << ia[ ix ]; |
| 51 |
|
| 52 |
if (ix % lineLength != lineLength-1 && |
| 53 |
ix != size-1) |
| 54 |
os << ", "; |
| 55 |
} |
| 56 |
os << " >\n"; |
| 57 |
} |
| 58 |
|
| 59 |
template <class Type> |
| 60 |
void Array<Type>::init(const Type *array, int sz) |
| 61 |
{ |
| 62 |
ia = new Type[size = sz]; |
| 63 |
|
| 64 |
for (int ix = 0; ix < size; ++ix) |
| 65 |
ia[ix] = (array!=0) ? array[ix] : (Type)0; |
| 66 |
} |
| 67 |
|
| 68 |
// --------------- Array_RC.h && Array_RC.cc ---------------- |
| 69 |
|
| 70 |
template <class Type> |
| 71 |
class Array_RC : public Array<Type> { |
| 72 |
public: |
| 73 |
Array_RC(const Type *ar, int sz); |
| 74 |
Type& operator[](int ix); |
| 75 |
}; |
| 76 |
|
| 77 |
template <class Type> |
| 78 |
Array_RC<Type>::Array_RC(const Type *ar, int sz) : Array<Type>(ar, sz) {} |
| 79 |
|
| 80 |
template <class Type> |
| 81 |
Type &Array_RC<Type>::operator[](int ix) { |
| 82 |
assert(ix >= 0 && ix < this->size); |
| 83 |
return this->ia[ix]; |
| 84 |
} |
| 85 |
|
| 86 |
// ------------------- Test routine ---------------------- |
| 87 |
|
| 88 |
template <class Type> |
| 89 |
void try_array( Array<Type> &iA ) |
| 90 |
{ |
| 91 |
cout << "try_array: initial array values:\n"; |
| 92 |
cout << iA << endl; |
| 93 |
} |
| 94 |
|
| 95 |
template <class Type> |
| 96 |
inline void |
| 97 |
try_array( Array_RC<Type> &rc ) |
| 98 |
{ |
| 99 |
try_array( ((Array<Type>&)rc) ); |
| 100 |
} |
| 101 |
|
| 102 |
int main() |
| 103 |
{ |
| 104 |
static int ia[10] = { 12, 7, 14, 9, 128, 17, 6, 3, 27, 5 }; |
| 105 |
Array_RC<int> iA(ia, 10); |
| 106 |
|
| 107 |
cout << "template Array_RC class" << endl; |
| 108 |
try_array(iA); |
| 109 |
|
| 110 |
return 0; |
| 111 |
} |
| 112 |
|
| 113 |
template class Array_RC<int>; |