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