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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!mcsun!sun4nl!star.cs.vu.nl!estel
  3. From: estel@cs.vu.nl (Stel E)
  4. Subject: Re: how to initilize an array inside of a class??
  5. Message-ID: <BxnAuM.51w@cs.vu.nl>
  6. Sender: news@cs.vu.nl
  7. Organization: Fac. Wiskunde & Informatica, VU, Amsterdam
  8. References: <BxL3t2.FKG@ns1.nodak.edu> <1992Nov12.184605.1@vax1.bham.ac.uk>
  9. Date: Fri, 13 Nov 1992 08:13:33 GMT
  10. Lines: 91
  11.  
  12. In article <1992Nov12.184605.1@vax1.bham.ac.uk> mccauleyba@vax1.bham.ac.uk (Brian McCauley) writes:
  13. >In article <BxL3t2.FKG@ns1.nodak.edu>, cooper@plains.NoDak.edu (Jeff Cooper) writes:
  14. > [ example class with array member ] 
  15. >> Now what I'd need to do is initilize the array 'some_ints' with some
  16. >> preset values...normally one would do it like:
  17. >> 
  18. >> int some_ints[3000] = { 1, 6, 34, 3, ... ,0 };
  19. >> 
  20. >> But my compiler complains about that, saying that I can't initialize
  21. >> class members there.  The only other way I can think of doing it is
  22. >> to do something like:
  23. >> 
  24. >> Y::Y()
  25. >> {
  26. >>     some_ints[0] = 1;
  27. >>     some_ints[1] = 6;
  28. >>     ...
  29. >>     some_ints[2999] = 0;
  30. >> }
  31. >> 
  32. >> Is this my only other option??
  33. >Yes I'm afraid it is. I asked a very similar question to this last week. I
  34. >would like to see the array handling in C++ tidied up a bit, but the weight of
  35. >oppinion is against me. If I had my way you would be able to say:
  36. >Y::Y() {some_ints={1,6, ... 0}; }
  37. >or 
  38. >Y::Y() : some_ints({1,6,...0}) {}
  39. >but nobody seems to support me.
  40. >-- 
  41. >    \\   ( )  NO BULLSHIT! from BAM (Brian McCauley)
  42. > .  _\\__[oo 
  43. >.__/  \\ /\@  E-mail: B.A.McCauley@bham.ac.uk
  44. >.  l___\\        Fax: +44 21 625 2175
  45. > # ll  l\\     Snail: 197 Harborne Lane, Birmingham, B29 6SS, UK
  46. >###LL  LL\\     ICBM: 52.5N 1.9W
  47.  
  48.  
  49. I'm not an expert in C(++) but why not use something like:
  50.  
  51. // ***************************************************************
  52. #include<iostream.h>
  53. #include<stdio.h>
  54.  
  55. #define    ARR_SIZE    10
  56.  
  57.  
  58. const int InitVal[ARR_SIZE] = {
  59.     10, 20, 30, 40, 50, 100, 90, 80, 70, 60
  60. };
  61.  
  62.  
  63. class X {
  64.     int    SomeInts[ARR_SIZE];
  65. public:
  66.         X();
  67.         ~X();
  68.     void    Write();
  69. };
  70.  
  71. X::X()
  72. {
  73.     cout << "X constructor" << endl;
  74.     memcpy(SomeInts, InitVal, ARR_SIZE * sizeof(int));
  75. }
  76.  
  77. X::~X()
  78. {
  79.     cout << "X destructor" << endl;
  80. }
  81.  
  82. void X::Write()
  83. {
  84.     for(int i=0;i<ARR_SIZE;i++)
  85.         cout << SomeInts[i] << "  ";
  86.     cout << endl;
  87. }
  88.  
  89. main()
  90. {
  91.     X    Some;
  92.  
  93.     Some.Write();
  94.  
  95.     return 0;
  96. }
  97. // ***************************************************************
  98.  
  99. It's just a silly program I know....
  100.  
  101. Good luck,                Erik Stel (estel@cs.vu.nl)
  102.