home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / cplus / 12450 < prev    next >
Encoding:
Text File  |  1992-08-17  |  3.4 KB  |  104 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!pilchuck!fnx!sgihbtn!dgeary
  3. From: dgeary@sierra.com (David Geary)
  4. Subject: Re: dynamic array of pointers to a class type
  5. Message-ID: <1992Aug17.143621.29059@sierra.com>
  6. Sender: news@sierra.com
  7. Nntp-Posting-Host: grumpy
  8. Organization: Sierra Geophysics,  Kirkland WA
  9. References: <ken.4@tlc-486.clemson.edu>
  10. Date: Mon, 17 Aug 1992 14:36:21 GMT
  11. Lines: 91
  12.  
  13. In article <ken.4@tlc-486.clemson.edu> ken@tlc-486.clemson.edu (Ken Bauman) writes:
  14. >
  15. >(Using Borland C++ 3.1)
  16. >
  17. >Got a small problem here.  I am trying in one class to create a list of 
  18. >pointers to another class.  The size of the list may vary widely upon 
  19. >creation of the class containing the list.  This seems obviously a job for 
  20. >the constructor.  However I can't quite seem to get it right.  In the 
  21. >following simple example the intent is for class B to create a list of 
  22. >pointers to class A.
  23. >
  24. >class A { int i; };
  25. >
  26. >class B
  27. >{
  28. >private:
  29. >   A *list[];  // list should be an open array of pointers to objects
  30. >               // of type class A
  31. >public:
  32. >   B(int);
  33. >};
  34. >
  35. >B::B (int size)
  36. >{
  37. >   list = new A *[size];  // this (I think) should allocate the space for
  38. >                          // an array of pointers to class A
  39. >   // However, I get a compiler error Lvalue required
  40. >}
  41. >
  42. >I can get around this by declaring in class B
  43. >   A **list;
  44. >instead of 
  45. >   A *list[];
  46. >but this seems rather ungracefull and unintuitive?  and perhaps bad 
  47. >programming practice.  I know that list is a pointer to an array of pointers 
  48. >and thus may be considered a pointer to a pointer but it doesn't seem to me 
  49. >to be quite the same thing.
  50. >
  51. >Can anyone provide some insight, both as to correctness and good programming 
  52. >practice?  Much thanks.
  53. >
  54. >Ken
  55. >
  56. > /---------------------------------------------------------\
  57. >/  Ken Bauman @         |    My opinions are irrelevant.    \
  58. >\  tlc-486.clemson.edu  |  Everything I say is fact.  NOT!  /
  59. > \---------------------------------------------------------/
  60.  
  61. Ken,
  62.  
  63.   You are trying to change the address of an array, (remember, list
  64. is an *array* of pointers - [] has precedence over *)  which is illegal.
  65. Imagine the havoc that would ensue, were one able to write the
  66. following code:
  67.  
  68. main()
  69. {
  70.   char  someArray[100];
  71.  
  72.   strcpy(someArray, "hello");
  73.  
  74.   someArray = 5;               // Illegal! [1]
  75.  
  76.   cout << someArray;           // What gets printed here?
  77. }
  78.  
  79.  
  80. [1] - Since the name of an array in a value context is the same as
  81.       the address of the first element in the array, what you are
  82.       saying is:
  83.  
  84.       &someArray[0] = 5;
  85.  
  86.       What would be the effect of this sly maneuver?  We're indicating
  87.       that we want that 'h' placed in memory location 5.  Do the
  88.       rest of the 100 chars get "moved" also?  What happens to whatever
  89.       was at memory address 5?  Due to such problems, the assignment
  90.       is disallowed.
  91.  
  92.   Therefore, what you *must* declare (it's not a matter of good/bad
  93. programming practice) is a *pointer*, which is an Lvalue, which means
  94. you can write to it (the L stands for Loadable - we can load the
  95. expression with a value - some prefer to think of the L as "left", as
  96. in it's on the left hand side of an assignment).  Arrays are
  97. Rvalues, which means they can only be *Read* from - some prefer
  98. to think of the R as "right", as in it's on the right hand
  99. side of an assignment.
  100. -- 
  101. David Geary               |   Seattle - America's most attractive city ... 
  102. Sierra Geophysics    |    
  103. dgeary@sierra.com    |   to the jetstream ;-(
  104.