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

D

doublemaster007

Hi All,

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??

Nasir
 
T

Triple-DES

Hi All,

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??

Nasir

You can use the following syntax

struct C
{
int a[10];
C() : a() {}
};

which will value-initialize all elements of a (set to zero for ints
and other scalar types).
 
D

doublemaster007

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??

You can use the following syntax

struct C
{
  int a[10];
  C() : a() {}

};

which will value-initialize all elements of a (set to zero for ints
and other scalar types).

Thank you. but what about char array? how do i make sure that all the
ele of char array is '\0'
 
T

Triple-DES

You can use the following syntax
struct C
{
  int a[10];
  C() : a() {}

which will value-initialize all elements of a (set to zero for ints
and other scalar types).

Thank you. but what about char array? how do i make sure that all the
ele of char array is '\0'

You can use the same syntax. Like I said, all elements will be set to
zero for scalar types. This includes chars.
 
T

tharinda.gl

Hi All,

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??

Nasir

Hi all,

This is one of the problems that I face regularly. We may use
memset to initilize such a integer array. But how can we create an
array of objects from a class which doesn't have a default
constructor? Earlier c++ compilers allowed us to use a syntax like
this

int aiMyArray[10](0); //Initialize all the array elements to zero. But
newer ones doesn't allow this, is there a reason to remove this
functionality?

Thanks,
Tharinda
 
T

tharinda.gl

If can do thisinta[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??

Hi all,

        This is one of the problems that I face regularly. We may use
memset to initilize such a integer array. But how can we create an
array of objects from a class which doesn't have a default
constructor? Earlier c++ compilers allowed us to use a syntax like
this

intaiMyArray[10](0); //Initializeall the array elements to zero. But
newer ones doesn't allow this, is there a reason to remove this
functionality?

Thanks,
Tharinda

I know memset is not recommended in c++, then what should we use?, i
hate to use vectors to implement a simple task like this since they
look nasty when debugging :(
 
B

Bo Persson

If can do thisinta[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??

Hi all,

This is one of the problems that I face regularly. We may use
memset to initilize such a integer array. But how can we create an
array of objects from a class which doesn't have a default
constructor? Earlier c++ compilers allowed us to use a syntax like
this

intaiMyArray[10](0); //Initializeall the array elements to zero.
But newer ones doesn't allow this, is there a reason to remove this
functionality?

Thanks,
Tharinda

I know memset is not recommended in c++, then what should we use?, i
hate to use vectors to implement a simple task like this since they
look nasty when debugging :(

Ok, so this isn't a language problem after all - what about using a
better debugger?

Some of them CAN show the contents of a std::vector.


Bo Persson
 
T

tharinda.gl

(e-mail address removed) wrote:
Hi All,
If can do thisinta[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??
Nasir
Hi all,
This is one of the problems that I face regularly. We may use
memset to initilize such a integer array. But how can we create an
array of objects from a class which doesn't have a default
constructor? Earlier c++ compilers allowed us to use a syntax like
this
intaiMyArray[10](0); //Initializeall the array elements to zero.
But newer ones doesn't allow this, is there a reason to remove this
functionality?
Thanks,
Tharinda
I know memset is not recommended in c++, then what should we use?, i
hate to use vectors to implement a simple task like this since they
look nasty when debugging :(

Ok, so this isn't a language problem after all - what about using a
better debugger?

Some of them CAN show the contents of a std::vector.

Bo Persson

Thanks Bo Persson, Can you mention such a debugger? I am using gdb 6.6
with g++ 3.4.2. I have seen MS Visual Studio 2008 doing exactly what
you have mentioned above. But we can't use it for back-end
development.

Thanks!
Tharinda
 
T

tharinda.gl

On Feb 16, 1:53 pm, (e-mail address removed) wrote:
(e-mail address removed) wrote:
Hi All,
If can do thisinta[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??
Nasir
Hi all,
This is one of the problems that I face regularly. We may use
memset to initilize such a integer array. But how can we create an
array of objects from a class which doesn't have a default
constructor? Earlier c++ compilers allowed us to use a syntax like
this
intaiMyArray[10](0); //Initializeall the array elements to zero.
But newer ones doesn't allow this, is there a reason to remove this
functionality?
Thanks,
Tharinda
I know memset is not recommended in c++, then what should we use?, i
hate to use vectors to implement a simple task like this since they
look nasty when debugging :(
Ok, so this isn't a language problem after all - what about using a
better debugger?
Some of them CAN show the contents of a std::vector.
Bo Persson

Thanks Bo Persson, Can you mention such a debugger? I am using gdb 6.6
with g++ 3.4.2. I have seen MS Visual Studio 2008 doing exactly what
you have mentioned above. But we can't use it for back-end
development.

Thanks!
Tharinda

Any answers to my previous question? Why the newer versions of
compilers don't allow us to initialize arrays like this.

int aiValues[10](-1);

This was a very helpful feature, there should be a strong reason to
remove such a functionality.

Thanks!
Tharinda
 
T

Triple-DES

Any answers to my previous question? Why the newer versions of
compilers don't allow us to initialize arrays like this.

int aiValues[10](-1);

This was a very helpful feature, there should be a strong reason to
remove such a functionality.

That would be a compiler-specific extension. It has never been
standard C++, as far as I know.
 
J

James Kanze

(e-mail address removed) wrote:
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 } ;
But newer ones doesn't allow this, is there a reason to remove
this functionality?

Nothing's been removed, to my knowledge.

If I understand correctly, C++0x will allow direct
list-initialization of aggregates in a constructor initializer,
e.g.:

class C
{
int a[ 3 ] ;
C() : a{ 1, 2, 3 } {}
} ;

will be legal, and will initialize a with the values 1, 2 and 3.
 
J

Jorgen Grahn

I know memset is not recommended in c++, then what should we use?

std::fill is type-safe and works on plain arrays as well as
std::vector and other things. Use that one instead of memset.

I would consider using memset only if it proved significantly faster
in a scenario where I needed extra speed.
i hate to use vectors to implement a simple task like this since they
look nasty when debugging :(

On the other hand, code which uses std::vector is less likely to *need*
debugging ... if I have to chose between readable code and readable
debugging, I chose the former.

/Jorgen
 
J

Juha Nieminen

Triple-DES said:
You can use the following syntax

struct C
{
int a[10];
C() : a() {}
};

which will value-initialize all elements of a (set to zero for ints
and other scalar types).

Where do you get this information?

Out of curiosity, I tried googling for this (ie. how to
default-initialize a member array using the constructor initialization
list) and could not find even one single page mentioning the syntax you
wrote above.
 
B

Bo Persson

On Feb 16, 1:53 pm, (e-mail address removed) wrote:
(e-mail address removed) wrote:
Hi All,
If can do thisinta[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??

Hi all,
This is one of the problems that I face regularly. We may use
memset to initilize such a integer array. But how can we create
an array of objects from a class which doesn't have a default
constructor? Earlier c++ compilers allowed us to use a syntax
like this
intaiMyArray[10](0); //Initializeall the array elements to zero.
But newer ones doesn't allow this, is there a reason to remove
this functionality?
Thanks,
Tharinda

I know memset is not recommended in c++, then what should we
use?, i hate to use vectors to implement a simple task like this
since they look nasty when debugging :(

Ok, so this isn't a language problem after all - what about using a
better debugger?

Some of them CAN show the contents of a std::vector.

Bo Persson

Thanks Bo Persson, Can you mention such a debugger? I am using gdb
6.6 with g++ 3.4.2. I have seen MS Visual Studio 2008 doing exactly
what you have mentioned above.

That's the one I was thinking about.

As a language fan :), I couldn't resist pointing out that this is a
tool problem, not a language problem. Fighting the language, to
accomodate the tools, is not fun at all.
But we can't use it for back-end
development.

Perhaps you could use it for debugging new code separately? If not,
you might be out of luck.


Bo Persson
 
T

tharinda.gl

Juha said:
Triple-DES said:
You can use the following syntax

struct C
{
int a[10];
C() : a() {}
};

which will value-initialize all elements of a (set to zero for ints
and other scalar types).

Where do you get this information?

Out of curiosity, I tried googling for this (ie. how to
default-initialize a member array using the constructor initialization
list) and could not find even one single page mentioning the syntax you
wrote above.

Juha Nieminen, I don't exactly remember the gcc version but it was
something like g++ 2.9.xx. I am really sure the syntax I have used
above is allowed there since I had to change it in all places when
porting it to a newer g++ version (3.4.3).
 
T

tharinda.gl

Juha said:
Triple-DES said:
You can use the following syntax
struct C
{
  int a[10];
  C() : a() {}
};
which will value-initialize all elements of a (set to zero for ints
and other scalar types).
  Where do you get this information?
  Out of curiosity, I tried googling for this (ie. how to
default-initialize a member array using the constructor initialization
list) and could not find even one single page mentioning the syntax you
wrote above.

Juha Nieminen, I don't exactly remember the gcc version but it was
something like g++ 2.9.xx. I am really sure the syntax I have used
above is allowed there since I had to change it in all places when
porting it to a newer g++ version (3.4.3).

And if you don't find any problems with the syntax above, why don't
you people suggest it as a way of initializing an array?
 
T

Triple-DES

Triple-DES said:
You can use the following syntax
struct C
{
int a[10];
C() : a() {}
};
which will value-initialize all elements of a (set to zero for ints
and other scalar types).

Where do you get this information?

Out of curiosity, I tried googling for this (ie. how to
default-initialize a member array using the constructor initialization
list) and could not find even one single page mentioning the syntax you
wrote above.

Fair enough, but did you actually try compiling it? :)

A mem-initializer has the following form:
mem-initializer-id ( expression-list[opt] )

Note that the expression-list is optional (which is sometimes
useful).

Now, according to [class.base.init]/3

(...)if the expression-list of the mem-initializer is omitted, the
base class or member subobject is value-initialized(...)

Meaning that the array is value-initialized in this case. Value-
initialization of an array value-initializes all its elements.
 
J

Juha Nieminen

Triple-DES said:
Fair enough, but did you actually try compiling it? :)

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.

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.
 
A

Alf P. Steinbach

* Juha Nieminen:
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.

So where *do* people get this kind of information?

Three places:

* The standard or e.g. a good textbook.
The standard or textbook tells you about the syntax "member()".

* Experience.
Experience tells you that e.g. MSVC 7.1 doesn't implement that correctly.

* Discussion, e.g. in newsgroups.
Just post an article claiming something is impossible and you'll have goaded
all the majority of readers who can't leave "that claim is WRONG!"[1] alone,
into supplying you with a variety of ways of achieving the something.

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.

Cheers, & hth.,

- Alf

Notes:
[1] <url: http://xkcd.com/386/>
 
T

Triple-DES

  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.

  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.

Oh, I thought you were simply asking for the chapter & verse. In that
case, my answer may have come across as somewhat insolent. I assure
you that was not my intention.

When in doubt (almost always), I usually reach for my PDF version of
the standard. I also have a dead tree version filled with post-it
notes.

The Google Groups search function is also very useful, but in this
particular case, it does seem like google is of little help.

And yes, I can definitely relate to the xkcd strip that Alf referred
to :)
 

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
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top