question on constant variables

Discussion in 'C++' started by vaclavpich@atlas.cz, Sep 9, 2008.

  1. Guest

    Hi,
    I have a question on constant variables.
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //
    #include <cstdlib>
    #include <iostream>

    using namespace std;

    const static char* STATIC_CONST_NAME = "Hello";
    const char* CONST_NAME = "Hello";

    int main(int argc, char *argv[])
    {
    cout << STATIC_CONST_NAME << endl;
    cout << CONST_NAME << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
    }

    Can anybody explain to me different between (const static char*) and
    (const char*) ?
    Thanks.
     
    , Sep 9, 2008
    #1
    1. Advertising

  2. mqrk Guest

    It seems your question is actually about static variables. More
    specifically: global static variables.

    Using static gives the variable internal linkage. It's use in this
    manner is deprecated as you can achieve the same effect by declaring
    it in an anonymous namespace. Technically, it won't have internal
    linkage, but there's no way to refer to it from another compilation
    unit.
     
    mqrk, Sep 9, 2008
    #2
    1. Advertising

  3. wrote:
    > ...
    > const static char* STATIC_CONST_NAME = "Hello";
    > const char* CONST_NAME = "Hello";
    > ...
    > Can anybody explain to me different between (const static char*) and
    > (const char*) ?


    Keyword 'static' used in a file-scope declaration of an object gives
    this object internal linkage, which means that the object name becomes
    hidden from other translation units.

    But in your particular case there's no difference whatsoever. In C++
    'const' objects declared in file scope have internal linkage by default.
    Which means that the above keyword 'static' is entirely superfluous.
    Both declarations are absolutely equivalent, with only the object name
    being different.

    As a side note, since string literals are not modifiable in C++, I'd
    recommend using pointers of pointers-to-const-char type with string literals

    const static char* const STATIC_CONST_NAME = "Hello";
    const char* const CONST_NAME = "Hello";

    (note the extra inner 'const's).

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Sep 9, 2008
    #3
  4. Andrey Tarasevich wrote:
    >
    > As a side note, since string literals are not modifiable in C++, I'd
    > recommend using pointers of pointers-to-const-char type with string
    > literals
    >
    > const static char* const STATIC_CONST_NAME = "Hello";
    > const char* const CONST_NAME = "Hello";
    >
    > (note the extra inner 'const's).
    >


    Oops, sorry, scratch that... Of course, the inner 'const's mean
    something completely different. The outer 'const's take care of the
    issue I was addressing.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Sep 9, 2008
    #4
  5. Guest

    On 9 Sep, 21:07, Andrey Tarasevich <>
    wrote:
    > wrote:
    > > ...
    > > const static char* STATIC_CONST_NAME = "Hello";
    > > const char*          CONST_NAME               = "Hello";
    > > ...
    > > Can anybody explain to me different between (const static char*) and
    > > (const char*) ?

    >
    > Keyword 'static' used in a file-scope declaration of an object gives
    > this object internal linkage, which means that the object name becomes
    > hidden from other translation units.
    >
    > But in your particular case there's no difference whatsoever. In C++
    > 'const' objects declared in file scope have internal linkage by default.
    > Which means that the above keyword 'static' is entirely superfluous.
    > Both declarations are absolutely equivalent, with only the object name
    > being different.
    >
    > As a side note, since string literals are not modifiable in C++, I'd
    > recommend using pointers of pointers-to-const-char type with string literals
    >
    >    const static char* const STATIC_CONST_NAME = "Hello";
    >    const char*        const CONST_NAME        = "Hello";
    >
    > (note the extra inner 'const's).


    It's late, so I may not be thinking straight, but - don't the 'const's
    at the front do what you are intending here, making the strings
    unwriteable-to? And given that (ie that the 'const's at the front
    don't make the variables themselves constant) does that mess up the
    explanation you gave above?
     
    , Sep 9, 2008
    #5
  6. wrote:
    >>> ...
    >>> Can anybody explain to me different between (const static char*) and
    >>> (const char*) ?

    >> Keyword 'static' used in a file-scope declaration of an object gives
    >> this object internal linkage, which means that the object name becomes
    >> hidden from other translation units.
    >>
    >> But in your particular case there's no difference whatsoever. In C++
    >> 'const' objects declared in file scope have internal linkage by default.
    >> Which means that the above keyword 'static' is entirely superfluous.
    >> Both declarations are absolutely equivalent, with only the object name
    >> being different.
    >>
    >> As a side note, since string literals are not modifiable in C++, I'd
    >> recommend using pointers of pointers-to-const-char type with string literals
    >>
    >> const static char* const STATIC_CONST_NAME = "Hello";
    >> const char* const CONST_NAME = "Hello";
    >>
    >> (note the extra inner 'const's).

    >
    > It's late, so I may not be thinking straight, but - don't the 'const's
    > at the front do what you are intending here, making the strings
    > unwriteable-to? And given that (ie that the 'const's at the front
    > don't make the variables themselves constant) does that mess up the
    > explanation you gave above?


    Yes, you are absolutely right. I already noticed that the last part of
    my message was wrong, but I missed the fact that it also invalidates the
    whole thing I wrote originally (and it's not even late here). Thanks for
    pointing this out.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Sep 9, 2008
    #6
  7. James Kanze Guest

    On Sep 9, 9:38 pm, wrote:

    > I have a question on constant variables.
    > /////////////////////////////////////////////
    > //
    > #include <cstdlib>
    > #include <iostream>


    > using namespace std;


    > const static char* STATIC_CONST_NAME = "Hello";


    This is a legal, but a very unusual way of declaring the
    variable. Storage specifiers (static, extern, etc.) always come
    first. (According to the C99 standard, "The placement of a
    storage-class specifier other than at the beginning of the
    declaration specifiers in a declaration is an obsolescent
    feature." Formally, this doesn't affect C++, but practically,
    putting static anywhere but at the beginning of a declaration
    causes confusion for the readers.)

    > const char* CONST_NAME = "Hello";


    > int main(int argc, char *argv[])
    > {
    > cout << STATIC_CONST_NAME << endl;
    > cout << CONST_NAME << endl;
    > system("PAUSE");
    > return EXIT_SUCCESS;
    > }


    > Can anybody explain to me different between (const static
    > char*) and (const char*) ?


    As others have pointed out, the difference here is linkage, and
    it has nothing to do with the const here. Your program doesn't
    have any const variables; only non-const variables which point
    to const.

    Note that the default linkage of a const variable is internal,
    so:

    char const* const name1 = "..." ;

    has internal linkage, even without the "static". To give it
    external linkage, you need the keyword "extern", e.g.:

    extern char const* const name1 = "..." ;

    In summary:

    // declaration linkage
    // -----------------------------------------------------
    char const* name1 = "..." ; // external
    static char const* name2 = "..." ; // internal
    extern char const* name3 = "..." ; // external
    char const* const name1 = "..." ; // internal
    static char const* const name2 = "..." ; // internal
    extern char const* const name3 = "..." ; // external

    (And I know, it's not consistent. But hey, that's C++.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Sep 10, 2008
    #7
    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. Christopher M. Lusardi
    Replies:
    1
    Views:
    4,132
  2. Martin Magnusson
    Replies:
    2
    Views:
    534
    John Harrison
    Oct 8, 2004
  3. Tor Erik Soenvisen
    Replies:
    14
    Views:
    591
    Tim Roberts
    Nov 23, 2006
  4. Replies:
    4
    Views:
    357
    Keith Thompson
    Dec 14, 2006
  5. Replies:
    13
    Views:
    13,026
    Kai-Uwe Bux
    Jan 22, 2007
Loading...

Share This Page