home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!snorkelwacker.mit.edu!ira.uka.de!ira.uka.de!slsvaat!josef!kanze
- From: kanze@us-es.sel.de (James Kanze)
- Subject: Re: how to initilize an array inside of a class??
- In-Reply-To: cooper@plains.NoDak.edu's message of 12 Nov 92 03:46:14 GMT
- Message-ID: <KANZE.92Nov13193446@slsvhat.us-es.sel.de>
- Sender: news@us-es.sel.de
- Organization: SEL
- References: <BxL3t2.FKG@ns1.nodak.edu>
- Date: 13 Nov 92 19:34:46
- Lines: 76
-
- In article <BxL3t2.FKG@ns1.nodak.edu> cooper@plains.NoDak.edu (Jeff
- Cooper) writes:
-
- |> I have a bit of a problem that I can't really see an elegant solution
- |> for and I hope someone can point me in the right direction. I have
- |> a class that looks something like this:
-
- |> class Y : public X
- |> {
- |> public:
- |> int some_ints[3000];
- |> Y();
- |> ...
- |> };
-
- |> 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?? If it is, it's not really all that bad
- |> as this code is being generated by another program, so I won't have to
- |> type in 3000 entries like that, but I was hoping for a more elegant
- |> solution...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??
-
- |> Any Ideas?
-
- First, note that there is a slight difference in declaring an array
- outside of a class, and declaring one inside the class. The array
- declared (and initialized) outside the class defines a single instance
- of a variable, so a single initialization is in order. The array
- declared inside the class does not define an instance of a variable;
- the array will be instantiated once for every instance of the class.
-
- So, the first question is: do you always want to initialize it in the
- same manner. And if so, do you ever change it later.
-
- If the answer to the latter question is false, then maybe what you
- want is a static class member; that is, a member common to all
- instances of the class. In this case, the solution is easy:
-
- class Y
- {
- public :
- static int someInts[ 3000 ] ;
- } ;
-
- and in one (and only one) source file:
-
- int Y::someInts[ 3000 ] = { 1 , 6 , ... , 0 } ;
-
- If you really need for each instance of the class to contain its own
- array member, which is always initialized with the same values, then
- my own solution would be to declare a private static member with the
- initial values, and use "memcpy" to copy it into the dynamic member.
- This is, of course, basically just doing what you suggested in another
- way, but I find it more readable.
- --
- James Kanze GABI Software, Sarl.
- email: kanze@us-es.sel.de 8 rue du Faisan
- 67000 Strasbourg
- France
-