From: srheintze@happy.colorado.edu
Subject: Re: Class template problem
Organization: University of Colorado, Boulder
Date: 11 Dec 92 11:03:57 MDT

In article <1992Dec10.163436.7922@nntp.nta.no>, stein@hal.nta.no (Stein Kulseth FBA) writes:
> I am new to C++ and is currently testing out some of its features.
> Maybe someone could help me with the following problems regarding
> class templates. 
> 
> I am using the GNU g++ compilater v.2.3.2 on a SUN SPARC.
> 
> Consider the following small program defining an Array class template:
> 
> ___
> 
> #include <stdlib.h>
> #include <iostream.h>
> 
> template <class T>
> class Array {
> 	private:
> 		int sz;
> 		T* element;
> public:
> 		Array(int size) : sz(size) { element = new T[sz]; };
> 		~Array() { delete [] element; };
> 		T& operator [] (int i) { return element[i]; };
> } ;
> 
> 
> int main()
> {
> int i;	
> 	Array<int> a(2);
> 	a[0] = 1;
> 	a[1] = 3;
> 
> 	Array< char * > c(2);
> 	c[0] = "Hello";
> 	c[1] = "World";
> 
> 	for (i=0;i<2;i++) {
> 		cout << a[i] << "\n";			// OK, prints integers 	
> 		cout << c[i] << "\n";			// Prints pointer values (0x....) ???
> 		cout << (char *) c[i] << "\n";	// Prints strings
> 	}
> }
> 
> ---
> Main question:
> 	Why isn't c[i] recognised as a char * ???
> 	After all c *is* declared as Array< char * >, and the return
> 	value of [] is a reference to T, ie a reference to a char *.

     When I used a VMS port of G++ I had a similar problem with printing char
values.  That version of of G++ did not seem to understand that  char was a
distinct type (it is not a distinct type in C) so, like C it prompted char
constants to an int and printed the ASCII value instead of the character
itself.  This looks like a similar problem.  I do not have either problem on
Borland C++ or MicrosoftC++.

> 
> Secondary question:
> 	If the template argument is itself a class, and this class'
> 	constructor needs argument, how would this be specified?
> 
> 	Ex. I want to implement a 4x3 2D array of int using an Array
> 	of an Array of int:
> 
> 		Array< Array<int> >
> 
> 	However here I run into problems as I cannot specify the
> 	innermost array dimension. How would I go about this?
> 
> Post or reply by email as you see fit.
> Thank you.

  This is an ugly problem.  Twice I have posted queries on this questions and
twice I have received no replies.  I'm beginning to wonder if there is
something wrong with this program I use to post to the news groups. I'll tell
you what I know about it.

  See page 61 of the ARM:  "No initializers can be specified for arrays"
when using "new" to allocate arrays.  Normally we use "[]" (like char* x = new
char[strlen(s)+1]) to allocate an array and "()" (like String* y = new
String("initialvalue");) to specify an initial value. But for reasons that are
beyond me, Bjarne and Margaret decided that "()" and "[]" were going to be
mutually exclusive (can someone explain this to me?).

 So what is the wordaround?   Margaret and Bjarne suggest using global values.
I think this is an absurd suggestion.

 Another suggestion is to pass two arguments to your parameterized class like
this:

    Array< Array<int, 23>, 45> arra;

This works, but there is a type compatibility problem here!  How do I pass such
an object to a function as an argument when the function argument must
accomodate all two diminsonal arrays of ints?

				Sieg  


Newsgroups: comp.lang.c++