GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #11  
Old 11-Jun-2009, 17:42
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How to initialize an array in a class definition or in constructor?


Quote:
Originally Posted by L7Sqr
const is not static.
I agree. I didn't mean to imply that they are the same, but maybe it appeared that I was sliding into the mindset that they are. You are, of course, correct: they are not. Thank you for the tweak.

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:
Originally Posted by L7Sqr
For example:
....

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:
test_constructor.cpp: In constructor `Foo::Foo()': test_constructor.cpp:6: error: ISO C++ forbids compound-literals test_constructor.cpp: In constructor `Foo::Foo(int, int, int, int)': test_constructor.cpp:7: error: ISO C++ forbids compound-literals

With g++ version 4.3.x compilers, without the -pedantic switch I get
Code:
test_constructor.cpp: In constructor 'Foo::Foo()': test_constructor.cpp:6: error: array used as initializer test_constructor.cpp: In constructor 'Foo::Foo(int, int, int, int)': test_constructor.cpp:7: error: array used as initializer

With g++ version 4.3.x compilers, with -pedantic I get
Code:
test_constructor.cpp: In constructor `Foo::Foo()': test_constructor.cpp:6: error: ISO C++ forbids compound-literals test_constructor.cpp: In constructor `Foo::Foo(int, int, int, int)': test_constructor.cpp:7: error: ISO C++ forbids compound-literals

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  
Old 11-Jun-2009, 20:24
L7Sqr L7Sqr is offline
Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 234
L7Sqr is a jewel in the roughL7Sqr is a jewel in the rough

Re: How to initialize an array in a class definition or in constructor?


Quote:
Originally Posted by davekw7x
unless I put in a -pedantic switch
I actually tested that with -W -Wall -Werror without issue so that I didnt make an example out of myself - So much for CYA
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  
Old 11-Jun-2009, 23:03
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How to initialize an array in a class definition or in constructor?


Quote:
Originally Posted by L7Sqr
...
I meant no offense by my comment ...
There was absolutely nothing wrong with your statement of fact or with the way you expressed it. My statement might have made it seem like the two are the same. It is certainly possible to have a static array that is not const.

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  
Old 12-Jun-2009, 10:32
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 803
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

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:
class Foo {
        const int twoD[2][2];
    public:
        Foo() : twoD((int[2][2]){{1,2},{3,4}}) {}
        Foo(int a,int b,int c,int d) : twoD((int[2][2]){{a,b},{c,d}}) {}

        void show () const {
            std::cout << "[[" << twoD[0][0] << "," << twoD[0][1] << "],["
            << twoD[1][0] << "," << twoD[1][1] << "]]" << std::endl;
        }
};
...with this statement:
CPP / C++ / C Code:
Foo() : twoD((int[2][2]){{1,2},{3,4}}) {}
What is that saying?
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  
Old 12-Jun-2009, 11:56
L7Sqr L7Sqr is offline
Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 234
L7Sqr is a jewel in the roughL7Sqr is a jewel in the rough

Re: How to initialize an array in a class definition or in constructor?


In the code snippet:
CPP / C++ / C Code:
Foo() : twoD((int[2][2]){{1,2},{3,4}}) {}

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  
Old 04-Sep-2009, 10:16
CPlus2Progammer CPlus2Progammer is offline
New Member
 
Join Date: Sep 2009
Posts: 2
CPlus2Progammer is on a distinguished road

Re: How to initialize an array in a class definition or in constructor?


CPP / C++ / C Code:
class Foo
{
    const int twoD[2][2] = { {0,1,2}, {3,4,5}, {5,6,7} };
};

if it must initialized within the constructor :

CPP / C++ / C Code:
class Foo
{
public:
      Foo();
     ~Foo();
private:
     const int twoD[2][2];
};

Foo::Foo()
{
    twoD[2][2] = { {0,1,2}, {3,4,5}, {5,6,7} };
};
i don't guarantee to compile well, but you can try it.
Last edited by admin : 05-Sep-2009 at 00:50. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #17  
Old 05-Sep-2009, 12:40
acuan's Avatar
acuan acuan is offline
New Member
 
Join Date: Sep 2009
Location: Indonesia
Posts: 1
acuan is on a distinguished road

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  
Old 05-Sep-2009, 13:14
L7Sqr L7Sqr is offline
Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 234
L7Sqr is a jewel in the roughL7Sqr is a jewel in the rough

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 GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 02:07.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.