Connecting Tech Pros Worldwide Forums | Help | Site Map

some questions about "const"

nick
Guest
 
Posts: n/a
#1: Nov 22 '05
1.int const x=5;
const int x=5;


2.const char *p=new char[20];
char const *p=new char[20];

the above pairs are equivalent?

thanks!

Mark P
Guest
 
Posts: n/a
#2: Nov 22 '05

re: some questions about "const"


nick wrote:[color=blue]
> 1.int const x=5;
> const int x=5;
>
>
> 2.const char *p=new char[20];
> char const *p=new char[20];
>
> the above pairs are equivalent?[/color]

yes
[color=blue]
>
> thanks![/color]
Neelesh
Guest
 
Posts: n/a
#3: Nov 22 '05

re: some questions about "const"



nick wrote:[color=blue]
> 1.int const x=5;
> const int x=5;
>
>
> 2.const char *p=new char[20];
> char const *p=new char[20];
>
> the above pairs are equivalent?
>
> thanks![/color]

Refer Stroupstrup's C++ Style and Technique FAQ
http://www.research.att.com/~bs/bs_f...constplacement

Neelesh
Guest
 
Posts: n/a
#4: Nov 22 '05

re: some questions about "const"



Neelesh wrote:[color=blue]
>
> Refer Stroupstrup's C++ Style and Technique FAQ[/color]
Sorry for the splling mistake, typing error.
Bjarne Stroustrup's C++ Style and Techniques FAQ

http://www.research.att.com/~bs/bs_f...constplacement

Ivan Vecerina
Guest
 
Posts: n/a
#5: Nov 22 '05

re: some questions about "const"


"nick" <i141802596@yahoo.com> wrote in message
news:dl4b0l$11e6$1@justice.itsc.cuhk.edu.hk...
: 1.int const x=5;
: const int x=5;
:
: 2.const char *p=new char[20];
: char const *p=new char[20];
:
: the above pairs are equivalent?

Yes, so which one you use is a matter of stylistic convention.

I only use a leading const for true compile-time constants:
const int x = 5;
const double pi = 3.1415926535;
(note that these are never composite types).


In all other cases, I put const *after* the type it affects:
char const *const p = new char[20];
int const h = getHeight( myWindow );


hth -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com


Howard
Guest
 
Posts: n/a
#6: Nov 22 '05

re: some questions about "const"



"nick" <i141802596@yahoo.com> wrote in message
news:dl4b0l$11e6$1@justice.itsc.cuhk.edu.hk...[color=blue]
> 1.int const x=5;
> const int x=5;
>
>
> 2.const char *p=new char[20];
> char const *p=new char[20];
>
> the above pairs are equivalent?
>
> thanks![/color]

Yes. Simple rule: const modifies what is to its immediate left. BUT...if
there's nothing to its left, then it modifies what is to its immediate
right.

-Howard


Closed Thread