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

some questions about "const"

P: n/a
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!
Nov 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
nick wrote:
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

thanks!

Nov 22 '05 #2

P: n/a

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


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

Nov 22 '05 #3

P: n/a

Neelesh wrote:

Refer Stroupstrup's C++ Style and Technique FAQ

Sorry for the splling mistake, typing error.
Bjarne Stroustrup's C++ Style and Techniques FAQ

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

Nov 22 '05 #4

P: n/a
"nick" <i1********@yahoo.com> wrote in message
news:dl***********@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
Nov 22 '05 #5

P: n/a

"nick" <i1********@yahoo.com> wrote in message
news:dl***********@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?

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
Nov 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.