How to initialize this kind 'int a[10]={0}' in member initialization?

J

James Kanze

I think I expressed myself very poorly and didn't convey the
proper message.
What I wanted to express was my marvel at such piece of
information about C++ which I didn't know and had never heard
about before. I have been programming C++ for over 15 years,
10 professionally, and I dare to say that I'm pretty fluent
with it, yet I had never heard of this way of
default-initializing a member array in the constructor
initialization list.

The fact that you've been programming C++ for over 15 years may
be the reason. I didn't know it either, until I saw Alf's
response. Historically, "classical" C++ didn't allow
initializing an array in an initialization list (and it didn't
have the concept of default initialization---if the type didn't
have a non-trivial constructor, using () to initialize it didn't
do anything). And of course, once you know that something
doesn't work, there's no reason to study it further.
So where *do* people get this kind of information? As I said,
I tried googling for it, out of curiosity, and couldn't find
even one single mention, so it doesn't seem to be a pretty
common knowledge.

Well, I'm not too sure what sort of expression you'd use in
Google. More to the point, none of the examples in the standard
and elsewhere that I've seen use arrays. Like you, I'd always
assumed that it was illegal. In respones to Alf, however, I
tried to find where in the standard it said that you couldn't
specify an array in the initializer list, and couldn't---the
restriction just isn't there. The description of initializer
lists just refers to §8.5, which treats an empty () specially
(so that it can work with arrays---where as no other (...) is
legal with an array). So I tried it with three different
compilers (g++, Sun CC and VC++), and it worked with all of
them.

I guess you're never too old to learn.
 
S

szymonwlodarski

If can do this int a[10]={0}; and char a[10] = {'\0'} which
will init all the element of array to zero or NULL cahr ,
then if i declare same in a class member how do i achive
this in member init list??
This is one of the problems that I face regularly. We may use
memset to initilize such a integer array.

But only for 0, and only for integral types.
But how can we create an array of objects from a class which
doesn't have a default constructor?

By using agglomerate initialization.  But you'll have to provide
an initializer for every element.
Earlier c++ compilers allowed us to use a syntax like this
int aiMyArray[10](0); //Initialize all the array elements to zero.

Earlier when?  CFront 2.1, g++ 1.49 and Zortech 1.0 (my first
C++ compilers) didn't allow it .  You could (and still can
write:

    int aiMyArray[ 10 ] = {} ;

and if class C doesn't have a default constructor (but has one
taking an int), you could, and still can write:

    Cc myArray[] = { 1, 2, 3 } ;

If it does you can't?

struct A
{
int x;
A() : x( 0 ) {}
A( int x1 ) : x( x1 ) {}
};

int
main()
{
A as[] = { 2, 4 };
}

Works for me.
 
J

James Kanze

Earlier when? CFront 2.1, g++ 1.49 and Zortech 1.0 (my first
C++ compilers) didn't allow it . You could (and still can
write:
int aiMyArray[ 10 ] = {} ;
and if class C doesn't have a default constructor (but has one
taking an int), you could, and still can write:
Cc myArray[] = { 1, 2, 3 } ;
If it does you can't?

I didn't say that. If it doesn't have a default constructor,
you can't write:

Cc myArray[] = {} ;

You can still use other forms, provided the necessary
constructors exist: a list of int's, if a converting constructor
for int exists, or a list of explicit Cc's, e.g.:

Cc myArray[] = { Cc( 1, 2 ), Cc( 3, 4 ) } ;

etc.
 
B

Brainsludge

James,

Two questions/points:

How do we know that the only legal initialization of an array in an
initialization list is ()? Does this mean that a member array can only
be initialized to {0} ?

Secondly, just as an FYI, it seems that VC++ only recently began
accepting the () usage. I learned this because I recently guessed that
I might be able to default-initialize an array using the initializer
list and got the following warning using VS8/VC++2005:

warning C4351: new behavior: elements of array 'test' will be default
initialized

MSDN has the following to say about this here:
http://msdn.microsoft.com/en-us/library/1ywe7hcy(VS.80).aspx

Thanks,
-Brian
 
S

szymonwlodarski

Earlier when?  CFront 2.1, g++ 1.49 and Zortech 1.0 (my first
C++ compilers) didn't allow it .  You could (and still can
write:
    int aiMyArray[ 10 ] = {} ;
and if class C doesn't have a default constructor (but has one
taking an int), you could, and still can write:
    Cc myArray[] = { 1, 2, 3 } ;
If it does you can't?

I didn't say that.

I know you didn't. Probably, I asked because you didn't simply write:
and if class C has a constructor taking an int. I thought that it may
be, for some reasons(?), prohibited by the Standard - implemented only
as an extension.
If it doesn't have a default constructor,
you can't write:

    Cc myArray[] = {} ;

Yes. It would not be very wise to allow something that is inherently
prevented.
 
J

Juha Nieminen

Triple-DES said:
Or more precisely, the standard doesn't explicitly prohibit it.

Does that mean that a compiler not supporting that syntax does
actually not break the standard, and consequently one cannot use the
syntax in portable programs?
 
T

Triple-DES

  Does that mean that a compiler not supporting that syntax does
actually not break the standard, and consequently one cannot use the
syntax in portable programs?

No, a compiler is required to support it, because it is required to
value-initialize any member, array or not, when the above syntax is
used.
 
J

James Kanze

Or more precisely, the standard doesn't explicitly prohibit
it.

No, the standard makes a special case for empty expression
lists. If the list is empty, the subobject is "value
initialized". Otherwise, the subobject is direct initialized.
The distinction is important, because there is no direct
initialization of an array.
 
T

Triple-DES

No, the standard makes a special case for empty expression
lists.  If the list is empty, the subobject is "value
initialized".  

Yes, I'm fully aware of that. After all, it was me who brought it up.
I read your answer as "Because the standard makes a special case for
[arrays]" (which it doesn't).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,163
Messages
2,570,897
Members
47,434
Latest member
TobiasLoan

Latest Threads

Top