By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,421 Members | 1,127 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,421 IT Pros & Developers. It's quick & easy.

constructor question

P: n/a
I write a class as below:

class c1 {

public:

c1(): a(2), c[4]('\0') {} //LINE1

private:

int a;
char c[5];
}

But LINE1 can not pass compiling.

I modify it as:
class c1 {

public:

c1(): a(2){
c[4] = '\0'; //LINE1
}

private:

int a;
char c[5];
}

It works. Is there a better way to write the constructor? Thanks.

Jun 13 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
ju******@gmail.com wrote:
I write a class as below:

class c1 {

public:

c1(): a(2), c[4]('\0') {} //LINE1
If you want to default-initialise your array, use the () syntax:

c1(): a(2), c() {}

There is no other option available to you. By doing "c()" you
set all members to zero.

private:

int a;
char c[5];
}

But LINE1 can not pass compiling.

I modify it as:
class c1 {

public:

c1(): a(2){
c[4] = '\0'; //LINE1
}

private:

int a;
char c[5];
}

It works. Is there a better way to write the constructor? Thanks.


See above. Unfortunately you don't have many choices. AFAIK, the
Committee is working on some improvements.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 13 '06 #2

P: n/a
> >
But LINE1 can not pass compiling.

I modify it as:
class c1 {

public:

c1(): a(2){
c[4] = '\0'; //LINE1
}

private:

int a;
char c[5];
}


If I only want to initialize the last element of the array c as a NULL
terminator, is there a better way to do it?

Jun 13 '06 #3

P: n/a
ju******@gmail.com wrote:
>
> But LINE1 can not pass compiling.
>
> I modify it as:
> class c1 {
>
> public:
>
> c1(): a(2){
> c[4] = '\0'; //LINE1
> }
>
> private:
>
> int a;
> char c[5];
> }
>


If I only want to initialize the last element of the array c as a NULL
terminator, is there a better way to do it?


No.
Jun 13 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.