[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

question on constant variables

vaclavpich

9/9/2008 7:38:00 PM

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.
6 Answers

mqrk

9/9/2008 7:58:00 PM

0

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.


Andrey Tarasevich

9/9/2008 8:08:00 PM

0

vaclavpich@atlas.cz 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

9/9/2008 8:34:00 PM

0

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

gw7rib

9/9/2008 8:36:00 PM

0

On 9 Sep, 21:07, Andrey Tarasevich <andreytarasev...@hotmail.com>
wrote:
> vaclavp...@atlas.cz 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?

Andrey Tarasevich

9/9/2008 10:13:00 PM

0

gw7rib@aol.com 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

James Kanze

9/10/2008 8:34:00 AM

0

On Sep 9, 9:38 pm, vaclavp...@atlas.cz 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:james.kanze@gmail.com
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