some questions about "const"

Discussion in 'C++' started by nick, Nov 12, 2005.

  1. nick

    nick Guest

    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!
     
    nick, Nov 12, 2005
    #1
    1. Advertising

  2. nick

    Mark P Guest

    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!
     
    Mark P, Nov 12, 2005
    #2
    1. Advertising

  3. nick

    Neelesh Guest

    Neelesh, Nov 12, 2005
    #3
  4. nick

    Neelesh Guest

    Neelesh, Nov 12, 2005
    #4
  5. "nick" <> wrote in message
    news:dl4b0l$11e6$...
    : 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
     
    Ivan Vecerina, Nov 12, 2005
    #5
  6. nick

    Howard Guest

    "nick" <> wrote in message
    news:dl4b0l$11e6$...
    > 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
     
    Howard, Nov 12, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    11
    Views:
    1,108
  2. Javier
    Replies:
    2
    Views:
    565
    James Kanze
    Sep 4, 2007
  3. 0m
    Replies:
    26
    Views:
    1,122
    Tim Rentsch
    Nov 10, 2008
  4. fungus
    Replies:
    13
    Views:
    892
    fungus
    Oct 31, 2008
  5. Replies:
    2
    Views:
    541
    Andrew Koenig
    Feb 9, 2009
Loading...

Share This Page