home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!mcsun!sun4nl!star.cs.vu.nl!estel
- From: estel@cs.vu.nl (Stel E)
- Subject: Re: how to initilize an array inside of a class??
- Message-ID: <BxnAuM.51w@cs.vu.nl>
- Sender: news@cs.vu.nl
- Organization: Fac. Wiskunde & Informatica, VU, Amsterdam
- References: <BxL3t2.FKG@ns1.nodak.edu> <1992Nov12.184605.1@vax1.bham.ac.uk>
- Date: Fri, 13 Nov 1992 08:13:33 GMT
- Lines: 91
-
- In article <1992Nov12.184605.1@vax1.bham.ac.uk> mccauleyba@vax1.bham.ac.uk (Brian McCauley) writes:
- >In article <BxL3t2.FKG@ns1.nodak.edu>, cooper@plains.NoDak.edu (Jeff Cooper) writes:
- > [ example class with array member ]
- >> Now what I'd need to do is initilize the array 'some_ints' with some
- >> preset values...normally one would do it like:
- >>
- >> int some_ints[3000] = { 1, 6, 34, 3, ... ,0 };
- >>
- >> But my compiler complains about that, saying that I can't initialize
- >> class members there. The only other way I can think of doing it is
- >> to do something like:
- >>
- >> Y::Y()
- >> {
- >> some_ints[0] = 1;
- >> some_ints[1] = 6;
- >> ...
- >> some_ints[2999] = 0;
- >> }
- >>
- >> Is this my only other option??
- >Yes I'm afraid it is. I asked a very similar question to this last week. I
- >would like to see the array handling in C++ tidied up a bit, but the weight of
- >oppinion is against me. If I had my way you would be able to say:
- >Y::Y() {some_ints={1,6, ... 0}; }
- >or
- >Y::Y() : some_ints({1,6,...0}) {}
- >but nobody seems to support me.
- >--
- > \\ ( ) NO BULLSHIT! from BAM (Brian McCauley)
- > . _\\__[oo
- >.__/ \\ /\@ E-mail: B.A.McCauley@bham.ac.uk
- >. l___\\ Fax: +44 21 625 2175
- > # ll l\\ Snail: 197 Harborne Lane, Birmingham, B29 6SS, UK
- >###LL LL\\ ICBM: 52.5N 1.9W
- >
-
-
- I'm not an expert in C(++) but why not use something like:
-
- // ***************************************************************
- #include<iostream.h>
- #include<stdio.h>
-
- #define ARR_SIZE 10
-
-
- const int InitVal[ARR_SIZE] = {
- 10, 20, 30, 40, 50, 100, 90, 80, 70, 60
- };
-
-
- class X {
- int SomeInts[ARR_SIZE];
- public:
- X();
- ~X();
- void Write();
- };
-
- X::X()
- {
- cout << "X constructor" << endl;
- memcpy(SomeInts, InitVal, ARR_SIZE * sizeof(int));
- }
-
- X::~X()
- {
- cout << "X destructor" << endl;
- }
-
- void X::Write()
- {
- for(int i=0;i<ARR_SIZE;i++)
- cout << SomeInts[i] << " ";
- cout << endl;
- }
-
- main()
- {
- X Some;
-
- Some.Write();
-
- return 0;
- }
- // ***************************************************************
-
- It's just a silly program I know....
-
- Good luck, Erik Stel (estel@cs.vu.nl)
-