![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#1
|
|||
|
|||
How to initialize an array in a class definition or in constructor?Let's say I have a class called Foo having a constant 2D integer array. And I want to initialize its values in the class definition or constructor... How should I do this? I always get an error when I try initializing it in the declaration or in the constructor
INITIALIZING IN THE DEFINITION CPP / C++ / C Code:
I get a syntax error when I do this. INITIALIZING IN THE CONSTRUCTOR CPP / C++ / C Code:
I also get an error when I do this, and when I try to declare it as extern in the definition, it doesn't help... anyone can help me out? |
|||
|
#2
|
|||
|
|||
|
you can't define anything in a class. you may only declare it. that's why constructor is a good idea. However since your array has already been declared in the class, you may not use the initializer list. Here's the correct way:
CPP / C++ / C Code:
__________________
spasms!!! |
|
#3
|
|||
|
|||
|
hey thanks for the reply!
I guess there's no easier way to do it... I wanted to make my code clean. Thanks again Quote:
|
|
#4
|
||||
|
||||
|
Quote:
I don't think this is possible. C++ does not allow you to initialize a function member which in turn would make that static inside a class. A class is not meant to have any static variables. The only time that you can initialize an array is at the time that you define it. For that reason, the second one is invalid. The only work around that I can see is to assign the integers one at a time... Someone else may have an idea though... |
|
#5
|
||||
|
||||
|
assigning the values using a loop, within your constructor works. I've done it before
|
|
#6
|
|||
|
|||
|
Thanks for all the help guys. I fixed it by looping in the constructor
|
|
#7
|
|||
|
|||
Re: How to initialize an array in a class definition or in constructor?actually its possible to write
CPP / C++ / C Code:
|
|
#8
|
|||
|
|||
Re: How to initialize an array in a class definition or in constructor?Can you give us a complete example of how you can get that to work?
|
|
#9
|
|||
|
|||
Re: How to initialize an array in a class definition or in constructor?Since someone decided to revive this five-year-old thread, I thought I would add my .02 euros to try to throw some light on the original question.
The Original Poster wanted to initialize a const array in his class. Now, if an array is const, it can't be changed, therefore all objects will have the same array values, so a static member is appropriate. Regardless of opinions of others who said it can't be done and who said things like "classes aren't supposed to have" such stuff, It can be done, and, if you need it, I'll show a way. Bottom line: Declare the array static. Static arrays, like any other static class variables, have to be initialized somewhere outside the class definition (not in the constructor). That's all there is to it: CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
Here's a run Code:
This simple example gracelessly bails out of the program when invalid array index values are entered by the user. A more attractive solution would have the access function throw an exception with a message that the user program could catch and recover without aborting the program, but the approach using the static array would be the same. Regards, Dave |
|
#10
|
|||
|
|||
Re: How to initialize an array in a class definition or in constructor?Quote:
For example: CPP / C++ / C Code:
Quote:
The initializer list can be used specifically for situations like this (base constructors, const members, etc). __________________
My personal site: Utilities for text processing, debugging, testing and plotting |
Recent GIDBlog
Programming ebook direct download available by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| shorthest path-another way | Pandiani | C++ Forum | 6 | 09-May-2004 20:51 |
| Creating 2d array | madlynzz | C++ Forum | 1 | 20-Mar-2004 21:15 |
| Speed up C++ code about 3d array! | Truong Son | C++ Forum | 0 | 16-Mar-2004 22:52 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The