![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#11
|
|||
|
|||
Re: How to initialize an array in a class definition or in constructor?Quote:
However... I was going by the first post in the thread, where the Original Poster indicated that it is desired to declare a const array and initialize it with a set of bracketed constant literals in the class definition. As the various other posts from the year 2004 pointed out, you can't do it that way. You can't do it in a constructor either. (Not with standard C++, that is.) To me the original post indicates that it is desired for the array contents be the same for all objects (why else would he want to initialize a const array in the class definition?), and that is what is meant by a static data member. He wanted const. I added static. If they were the same, then I would have tried one or the other, not both. I showed how to declare and initialize a static const array with a separate statement outside the class definition. Quote:
Your example code compiles with my g++ version 3 and version 4.1.2 compilers unless I put in a -pedantic switch, in which case I get the following errors Code:
With g++ version 4.3.x compilers, without the -pedantic switch I get Code:
With g++ version 4.3.x compilers, with -pedantic I get Code:
With the several Borland and Microsoft compilers on my various systems I get lots of syntax errors. Not a single success. Bottom line: I haven't gone so far as to look up the exact paragraphs in the C++ Language Standard document which seem to be violated, but I don't think your example is standard C++. Regards, Dave Last edited by davekw7x : 11-Jun-2009 at 18:49.
|
|||
|
#12
|
|||
|
|||
Re: How to initialize an array in a class definition or in constructor?Quote:
You're correct; it looks like, strictly speaking, that is an invalid initializer which probably explains why I needed to cast to make it work. But I was commenting more on the issue of const members vs. static members. You statement, to me, implied that since something is const it must be the same for all classes and might as well be static. I was just pointing out that const members can be initialized in a class constructor and therefore need not be static - its actually a handy fact to know in some cases. I meant no offense by my comment - I tend to be terse - its a personality flaw. __________________
My personal site: Utilities for text processing, debugging, testing and plotting |
|
#13
|
|||
|
|||
Re: How to initialize an array in a class definition or in constructor?Quote:
Your code utilized one of many GNU language extensions that come and go from time to time, and, in fact, future revisions of the language may very well allow initialization of arrays in constructors. I have recently been involved in a project that was delayed considerably since current compilers reject certain "helpful" language "enhancements" that were extensively used on the original C code. Thanks. Dave Last edited by davekw7x : 12-Jun-2009 at 00:04.
|
|
#14
|
|||
|
|||
Re: How to initialize an array in a class definition or in constructor?I'm getting this for the most part but I don't understand what L7Sqr is doing in here:
CPP / C++ / C Code:
CPP / C++ / C Code:
I see the cast you spoke of (int[2][2]) It looks like it might be a constructor ? I see a single colon is used for a derived class, does that have anything to do with it? I need a vowel... thanks |
|
#15
|
|||
|
|||
Re: How to initialize an array in a class definition or in constructor?In the code snippet:
CPP / C++ / C Code:
The Foo() is the constructor for the class. In C++ you can provide an initializer list directly preceding the definition body for a constructor, it is signaled by inserting the colon. The twoD((int[2][2]){{1,2},{3,4}}) is the constructor for the internal twoD member. Since only supplying {{1,2},{3,4}} is a syntax error I tell the compiler to treat that construct as a int[2][2] by casting it. As for {{1,2},{3,4}} itself, it is a shorthand (see implications of portability in preceding posts) for declaring a compound literal (lists within lists). It is akin to doingint a[10] = {0}; but with nesting. __________________
My personal site: Utilities for text processing, debugging, testing and plotting |
|
#16
|
|||
|
|||
Re: How to initialize an array in a class definition or in constructor?CPP / C++ / C Code:
if it must initialized within the constructor : CPP / C++ / C Code:
Last edited by admin : 05-Sep-2009 at 00:50.
Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
|
|
#17
|
||||
|
||||
Re: How to initialize an array in a class definition or in constructor?looks find but something bugs found in CPlus2Programmer code, especially initializing a const int twoD within the Foo Constructor i think it's not possible. why ?? because a const can initialize only once, and it could not be assigned to any value anymore. so the coding should do like this :
class Foo { public: Foo(); ~Foo(); }; Foo::Foo() { const int twoD[2][2] = { {0,1,2}, {3,4,5}, {5,6,7} }; } Ok ?? Right ?? |
|
#18
|
|||
|
|||
Re: How to initialize an array in a class definition or in constructor?@CPlus2Progammer: No. For all the reasons stated in the previous posts. Read this thread from the beginning and understand the explanations and details before posting.
Also, you are introducing some serious concerns in that you are initializing an array of size 2 with 3 elements. @acuan: No. The const variable you show there will go out of scope when the constructor is complete. @both: Please do not post invalid code snippets and then ask if things are ok??. If you have a question of your own, start a new thread. If you have something meaningful to add to an existing thread do so in a timely manner not after the thread has become dormant and the problem solved or question answered. __________________
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