Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Initialization of const objects.

Reply
Thread Tools

Initialization of const objects.

 
 
zouyongbin
Guest
Posts: n/a
 
      11-07-2006
The code segment like below will fail at compilation:

int t = 5;
const int ix = t;
char cx[ix] = "hell";

while that like below is correct:

int t = 5;
const int ix = 5;
char cx[iy] = "hell";

Can anybody give me a explanation about what is really happening here?

Thanks.

 
Reply With Quote
 
 
 
 
Kavya
Guest
Posts: n/a
 
      11-07-2006

zouyongbin wrote:
> The code segment like below will fail at compilation:
>
> int t = 5;
> const int ix = t;
> char cx[ix] = "hell";
>
> while that like below is correct:
>
> int t = 5;
> const int ix = 5;
> char cx[iy] = "hell";
>
> Can anybody give me a explanation about what is really happening here?
>


I am not sure but I think since t is not a compile time constant that
is why you are getting error in first code whereas 5 is a compile time
constant due to which you are not getting error in 2nd case.

 
Reply With Quote
 
 
 
 
Salt_Peter
Guest
Posts: n/a
 
      11-07-2006

zouyongbin wrote:
> The code segment like below will fail at compilation:
>
> int t = 5;
> const int ix = t;
> char cx[ix] = "hell";


This works fine, although rather strange.
char cx[] = "hell";
makes sense, and you can get its size with:
size_t size = sizeof(cx)/sizeof(char);

>
> while that like below is correct:
>
> int t = 5;
> const int ix = 5;
> char cx[iy] = "hell";


the above does not compile, iy is not declared nor defined.

>
> Can anybody give me a explanation about what is really happening here?
>


If your asking about the const int, there is no problem there:
const int ix(5); // will do

try:

#include <iostream>

int main()
{
char* p_s = "hell"; // pointer to a literal
std::cout << p_s << std::endl;
}

 
Reply With Quote
 
zouyongbin
Guest
Posts: n/a
 
      11-07-2006

Salt_Peter wrote:
> zouyongbin wrote:
> > The code segment like below will fail at compilation:
> >
> > int t = 5;
> > const int ix = t;
> > char cx[ix] = "hell";

>
> This works fine, although rather strange.
> char cx[] = "hell";
> makes sense, and you can get its size with:
> size_t size = sizeof(cx)/sizeof(char);


My compiler is gcc4.1.2, and I got a compile error here.

> >
> > while that like below is correct:
> >
> > int t = 5;
> > const int ix = 5;
> > char cx[iy] = "hell";

>
> the above does not compile, iy is not declared nor defined.


Sorry, it's a type error. The "iy" should be "ix" here.

What I am confused here is that whether "ix" is a constant seems to be
influenced by what is assigned to "ix". If "ix" is assigned by a
constant, like 5, it act like a constant. But if "ix" is assigned by a
non-constant such as "t" here, it can not be set as the dimension of an
array.
I think that is weird. Is there any trick here?

Thanks.

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      11-07-2006
zouyongbin wrote:
> Salt_Peter wrote:
>
>>zouyongbin wrote:
>>
>>>The code segment like below will fail at compilation:
>>>
>>>int t = 5;
>>>const int ix = t;
>>>char cx[ix] = "hell";

>>
>>This works fine, although rather strange.
>>char cx[] = "hell";
>>makes sense, and you can get its size with:
>>size_t size = sizeof(cx)/sizeof(char);

>
>
> My compiler is gcc4.1.2, and I got a compile error here.
>
>
>>>while that like below is correct:
>>>
>>>int t = 5;
>>>const int ix = 5;
>>>char cx[iy] = "hell";

>>
>>the above does not compile, iy is not declared nor defined.

>
>
> Sorry, it's a type error. The "iy" should be "ix" here.
>
> What I am confused here is that whether "ix" is a constant seems to be
> influenced by what is assigned to "ix". If "ix" is assigned by a
> constant, like 5, it act like a constant. But if "ix" is assigned by a
> non-constant such as "t" here, it can not be set as the dimension of an
> array.


That's because it isn't a compile time constant.

--
Ian Collins.
 
Reply With Quote
 
Salt_Peter
Guest
Posts: n/a
 
      11-07-2006

zouyongbin wrote:
> Salt_Peter wrote:
> > zouyongbin wrote:
> > > The code segment like below will fail at compilation:
> > >
> > > int t = 5;
> > > const int ix = t;
> > > char cx[ix] = "hell";

> >
> > This works fine, although rather strange.
> > char cx[] = "hell";
> > makes sense, and you can get its size with:
> > size_t size = sizeof(cx)/sizeof(char);

>
> My compiler is gcc4.1.2, and I got a compile error here.
>
> > >
> > > while that like below is correct:
> > >
> > > int t = 5;
> > > const int ix = 5;
> > > char cx[iy] = "hell";

> >
> > the above does not compile, iy is not declared nor defined.

>
> Sorry, it's a type error. The "iy" should be "ix" here.
>
> What I am confused here is that whether "ix" is a constant seems to be
> influenced by what is assigned to "ix". If "ix" is assigned by a
> constant, like 5, it act like a constant. But if "ix" is assigned by a
> non-constant such as "t" here, it can not be set as the dimension of an
> array.
> I think that is weird. Is there any trick here?
>
> Thanks.


Thats the compile time constant issue, fix:

int main()
{
int t(5);
const int ix(t);
char* p_cx = new char[ix];
delete [] p_cx;
}

or better yet, a std::string or std::vector.

 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      11-07-2006
zouyongbin wrote:
> The code segment like below will fail at compilation:
>
> int t = 5;
> const int ix = t;
> char cx[ix] = "hell";
>
> while that like below is correct:
>
> int t = 5;
> const int ix = 5;
> char cx[iy] = "hell";


(Assuming you meant 'ix' here).

> Can anybody give me a explanation about what is really happening here?


In C++ language only integral constant expressions (ICEs) can be used to specify
size in array type declarations. I won't include the full definition of ICE here
(look it up), but what applies to this case is that if you want an object of
'const int' type to act as an ICE, you have to make sure it that it itself is
initialized with an ICE.

In the second case this requirements is met. 'ix' is initialized with an
explicit '5'' '5' is an ICE. This means that 'ix' is itself an ICE and can be
used to declare array types.

In the first case the above requirement is not met. 'iy' is initialized with 't'
and 't' is not an ICE, meaning that 'iy' is not an ICE. Therefore, 'iy' cannot
be used to declare array types.

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<const MyType> Vs const vector<MyType> magnus.moraberg@gmail.com C++ 2 02-09-2009 10:45 PM
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM



Advertisments