some questions about "const"

N

nick

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!
 
I

Ivan Vecerina

: 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
 
H

Howard

nick said:
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!

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
 

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,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top