home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!snorkelwacker.mit.edu!ai-lab!life.ai.mit.edu!tmb
- From: tmb@arolla.idiap.ch (Thomas M. Breuel)
- Newsgroups: comp.lang.c++
- Subject: Re: how to initilize an array inside of a class??
- Date: 13 Nov 92 10:25:06
- Organization: IDIAP (Institut Dalle Molle d'Intelligence Artificielle
- Perceptive)
- Lines: 45
- Message-ID: <TMB.92Nov13102506@arolla.idiap.ch>
- References: <BxL3t2.FKG@ns1.nodak.edu>
- Reply-To: tmb@idiap.ch
- NNTP-Posting-Host: arolla.idiap.ch
- In-reply-to: cooper@plains.NoDak.edu's message of 12 Nov 92 03:46:14 GMT
-
- int some_ints[3000] = { 1, 6, 34, 3, ... ,0 };
-
- But my compiler complains about that, saying that I can't initialize
- class members there.
-
- I believe GNU C accepts something like that, but your code would
- be unportable.
-
- 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??
-
- Well, not quite. You could write:
-
- static int some_ints_initial[] = { 1, 6, 34, 3, ..., 0};
-
- Y::Y() {
- copy_ints(some_ints,some_ints_initial,
- sizeof some_ints_initial/sizeof *some_ints_initial);
- }
-
- Would there be a performance hit doing it that way, I kinda
- think so as the first one is done at compile time, while the other is
- done at run-time, correct??
-
- Automatic variables always need to be initialized at runtime (that's a
- property of the machine, not of C). The version that I gave above
- that uses "copy_ints" and "some_ints_initial" is pretty much what the
- compiler would generate anyway, and presumably, back in the days when
- C still cared about such things, K&R wanted you to be aware of this
- cost. In the brave new world of C++ where O(n) copying operations (and
- a lot of other stuff) happen behind your back anyway, this attitude is
- probably an anachronism.
-
- Thomas.
-
-