home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / cplus / 17816 < prev    next >
Encoding:
Text File  |  1992-12-11  |  3.6 KB  |  114 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!cs.utexas.edu!sdd.hp.com!spool.mu.edu!agate!boulder!happy.colorado.edu!srheintze
  3. From: srheintze@happy.colorado.edu
  4. Subject: Re: Class template problem
  5. Message-ID: <1992Dec11.110357.1@happy.colorado.edu>
  6. Lines: 100
  7. Sender: news@colorado.edu (The Daily Planet)
  8. Nntp-Posting-Host: happy.uccs.edu
  9. Organization: University of Colorado, Boulder
  10. References: <1992Dec10.163436.7922@nntp.nta.no>
  11. Date: 11 Dec 92 11:03:57 MDT
  12. Lines: 100
  13.  
  14. In article <1992Dec10.163436.7922@nntp.nta.no>, stein@hal.nta.no (Stein Kulseth FBA) writes:
  15. > I am new to C++ and is currently testing out some of its features.
  16. > Maybe someone could help me with the following problems regarding
  17. > class templates. 
  18. > I am using the GNU g++ compilater v.2.3.2 on a SUN SPARC.
  19. > Consider the following small program defining an Array class template:
  20. > ___
  21. > #include <stdlib.h>
  22. > #include <iostream.h>
  23. > template <class T>
  24. > class Array {
  25. >     private:
  26. >         int sz;
  27. >         T* element;
  28. > public:
  29. >         Array(int size) : sz(size) { element = new T[sz]; };
  30. >         ~Array() { delete [] element; };
  31. >         T& operator [] (int i) { return element[i]; };
  32. > } ;
  33. > int main()
  34. > {
  35. > int i;    
  36. >     Array<int> a(2);
  37. >     a[0] = 1;
  38. >     a[1] = 3;
  39. >     Array< char * > c(2);
  40. >     c[0] = "Hello";
  41. >     c[1] = "World";
  42. >     for (i=0;i<2;i++) {
  43. >         cout << a[i] << "\n";            // OK, prints integers     
  44. >         cout << c[i] << "\n";            // Prints pointer values (0x....) ???
  45. >         cout << (char *) c[i] << "\n";    // Prints strings
  46. >     }
  47. > }
  48. > ---
  49. > Main question:
  50. >     Why isn't c[i] recognised as a char * ???
  51. >     After all c *is* declared as Array< char * >, and the return
  52. >     value of [] is a reference to T, ie a reference to a char *.
  53.  
  54.      When I used a VMS port of G++ I had a similar problem with printing char
  55. values.  That version of of G++ did not seem to understand that  char was a
  56. distinct type (it is not a distinct type in C) so, like C it prompted char
  57. constants to an int and printed the ASCII value instead of the character
  58. itself.  This looks like a similar problem.  I do not have either problem on
  59. Borland C++ or MicrosoftC++.
  60.  
  61. > Secondary question:
  62. >     If the template argument is itself a class, and this class'
  63. >     constructor needs argument, how would this be specified?
  64. >     Ex. I want to implement a 4x3 2D array of int using an Array
  65. >     of an Array of int:
  66. >         Array< Array<int> >
  67. >     However here I run into problems as I cannot specify the
  68. >     innermost array dimension. How would I go about this?
  69. > Post or reply by email as you see fit.
  70. > Thank you.
  71.  
  72.   This is an ugly problem.  Twice I have posted queries on this questions and
  73. twice I have received no replies.  I'm beginning to wonder if there is
  74. something wrong with this program I use to post to the news groups. I'll tell
  75. you what I know about it.
  76.  
  77.   See page 61 of the ARM:  "No initializers can be specified for arrays"
  78. when using "new" to allocate arrays.  Normally we use "[]" (like char* x = new
  79. char[strlen(s)+1]) to allocate an array and "()" (like String* y = new
  80. String("initialvalue");) to specify an initial value. But for reasons that are
  81. beyond me, Bjarne and Margaret decided that "()" and "[]" were going to be
  82. mutually exclusive (can someone explain this to me?).
  83.  
  84.  So what is the wordaround?   Margaret and Bjarne suggest using global values.
  85. I think this is an absurd suggestion.
  86.  
  87.  Another suggestion is to pass two arguments to your parameterized class like
  88. this:
  89.  
  90.     Array< Array<int, 23>, 45> arra;
  91.  
  92. This works, but there is a type compatibility problem here!  How do I pass such
  93. an object to a function as an argument when the function argument must
  94. accomodate all two diminsonal arrays of ints?
  95.  
  96.                 Sieg  
  97.  
  98.  
  99.