home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / cplus / 16229 < prev    next >
Encoding:
Text File  |  1992-11-14  |  3.0 KB  |  89 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!snorkelwacker.mit.edu!ira.uka.de!ira.uka.de!slsvaat!josef!kanze
  3. From: kanze@us-es.sel.de (James Kanze)
  4. Subject: Re: how to initilize an array inside of a class??
  5. In-Reply-To: cooper@plains.NoDak.edu's message of 12 Nov 92 03:46:14 GMT
  6. Message-ID: <KANZE.92Nov13193446@slsvhat.us-es.sel.de>
  7. Sender: news@us-es.sel.de
  8. Organization: SEL
  9. References: <BxL3t2.FKG@ns1.nodak.edu>
  10. Date: 13 Nov 92 19:34:46
  11. Lines: 76
  12.  
  13. In article <BxL3t2.FKG@ns1.nodak.edu> cooper@plains.NoDak.edu (Jeff
  14. Cooper) writes:
  15.  
  16. |> I have a bit of a problem that I can't really see an elegant solution
  17. |> for and I hope someone can point me in the right direction.  I have
  18. |> a class that looks something like this:
  19.  
  20. |> class Y : public X
  21. |> {
  22. |>     public:
  23. |>         int some_ints[3000];
  24. |>         Y();
  25. |>         ...
  26. |> };
  27.  
  28. |> Now what I'd need to do is initilize the array 'some_ints' with some
  29. |> preset values...normally one would do it like:
  30.  
  31. |> int some_ints[3000] = { 1, 6, 34, 3, ... ,0 };
  32.  
  33. |> But my compiler complains about that, saying that I can't initialize
  34. |> class members there.  The only other way I can think of doing it is
  35. |> to do something like:
  36.  
  37. |> Y::Y()
  38. |> {
  39. |>     some_ints[0] = 1;
  40. |>     some_ints[1] = 6;
  41. |>     ...
  42. |>     some_ints[2999] = 0;
  43. |> }
  44.  
  45. |> Is this my only other option??  If it is, it's not really all that bad
  46. |> as this code is being generated by another program, so I won't have to
  47. |> type in 3000 entries like that, but I was hoping for a more elegant
  48. |> solution...Would there be a performance hit doing it that way, I kinda
  49. |> think so as the first one is done at compile time, while the other is
  50. |> done at run-time, correct??
  51.  
  52. |> Any Ideas?
  53.  
  54. First, note that there is a slight difference in declaring an array
  55. outside of a class, and declaring one inside the class.  The array
  56. declared (and initialized) outside the class defines a single instance
  57. of a variable, so a single initialization is in order.  The array
  58. declared inside the class does not define an instance of a variable;
  59. the array will be instantiated once for every instance of the class.
  60.  
  61. So, the first question is: do you always want to initialize it in the
  62. same manner.  And if so, do you ever change it later.
  63.  
  64. If the answer to the latter question is false, then maybe what you
  65. want is a static class member; that is, a member common to all
  66. instances of the class.  In this case, the solution is easy:
  67.  
  68.     class Y
  69.     {
  70.     public :
  71.         static int someInts[ 3000 ] ;
  72.     } ;
  73.  
  74. and in one (and only one) source file:
  75.  
  76.     int        Y::someInts[ 3000 ] = { 1 , 6 , ... , 0 } ;
  77.  
  78. If you really need for each instance of the class to contain its own
  79. array member, which is always initialized with the same values, then
  80. my own solution would be to declare a private static member with the
  81. initial values, and use "memcpy" to copy it into the dynamic member.
  82. This is, of course, basically just doing what you suggested in another
  83. way, but I find it more readable.
  84. --
  85. James Kanze            GABI Software, Sarl.
  86. email: kanze@us-es.sel.de    8 rue du Faisan
  87.                 67000 Strasbourg
  88.                 France
  89.