OSDN Git Service

Formatting fixes.
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / g++.old-deja / g++.law / visibility13.C
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 Type>
19 class Array { // { dg-error "" } .struct Array_RC redecl.*
20 friend class Array_RC;
21 public:
22     Array(const Type *ar, int sz) { init(ar,sz); }
23     virtual ~Array() { delete [] ia; }
24     virtual void print(ostream& = cout);
25     virtual Type& operator[](int ix) { return ia[ix]; }
26 private:
27     void init(const Type*, int);
28     int size;
29     int *ia;
30 };
31
32 template <class Type>
33 ostream& operator<<( ostream& os, Array<Type>& ar )
34 {
35     ar.print(os);
36     return os;
37 }
38
39 template <class Type>
40 void Array<Type>::print(ostream& os)
41 {
42     const int lineLength = 12;
43
44     os << "( " << size << " )< ";
45     for (int ix = 0; ix < size; ++ix) {
46         if (ix % lineLength == 0 && ix) os << "\n\t";
47         os << ia[ ix ];
48
49         if (ix % lineLength != lineLength-1 &&
50             ix != size-1)
51             os << ", ";
52     }
53     os << " >\n";
54 }
55
56 template <class Type>
57 void Array<Type>::init(const Type *array, int sz)
58 {
59     ia = new Type[size = sz];
60
61     for (int ix = 0; ix < size; ++ix)
62         ia[ix] = (array!=0) ? array[ix] : (Type)0;
63 }
64
65 //     ---------------   Array_RC.h  &&  Array_RC.cc   ----------------
66
67 template <class Type>
68 class Array_RC : public Array<Type> {
69 public:
70     Array_RC(const Type *ar, int sz);
71     Type& operator[](int ix);
72 };
73
74 template <class Type>
75 Array_RC<Type>::Array_RC(const Type *ar, int sz) : Array<Type>(ar, sz) {}
76
77 template <class Type>
78 Type &Array_RC<Type>::operator[](int ix) {
79     assert(ix >= 0 && ix < size);// { dg-error "" } member .size.*
80     return ia[ix];// { dg-error "" } member .ia.*
81 }
82
83 //    -------------------   Test routine   ----------------------
84
85 template <class Type>
86 void try_array( Array<Type> &iA )
87 {
88     cout << "try_array: initial array values:\n";
89     cout << iA << endl;
90 }
91
92 template <class Type>
93 inline void
94 try_array( Array_RC<Type> &rc )
95 {
96     try_array( ((Array<Type>&)rc) );
97 }
98
99 int main()
100 {
101     static int ia[10] = { 12, 7, 14, 9, 128, 17, 6, 3, 27, 5 };
102     Array_RC<int> iA(ia, 10);
103
104     cout << "template Array_RC class" << endl;
105     try_array(iA);
106
107     return 0;
108 }
109
110 template class Array_RC<int>;