Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > question on constant variables

Reply
Thread Tools

question on constant variables

 
 
vaclavpich@atlas.cz
Guest
Posts: n/a
 
      09-09-2008
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.
 
Reply With Quote
 
 
 
 
mqrk
Guest
Posts: n/a
 
      09-09-2008
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.


 
Reply With Quote
 
 
 
 
Andrey Tarasevich
Guest
Posts: n/a
 
      09-09-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      09-09-2008
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
 
Reply With Quote
 
gw7rib@aol.com
Guest
Posts: n/a
 
      09-09-2008
On 9 Sep, 21:07, Andrey Tarasevich <(E-Mail Removed)>
wrote:
> (E-Mail Removed) 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?

 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      09-09-2008
(E-Mail Removed) 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
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      09-10-2008
On Sep 9, 9:38 pm, (E-Mail Removed) 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:(E-Mail Removed)
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"error C2057: expected constant expression", "error C2466: cannot allocate an array of constant size 0". Why doesn't my simple program work??? hn.ft.pris@gmail.com C++ 13 01-22-2007 02:03 PM
pointers to constant characters and constant pointers to characters sam_cit@yahoo.co.in C Programming 4 12-14-2006 11:10 PM
len(var) is [CONSTANT] equal to len(var) == [CONSTANT]? Tor Erik Soenvisen Python 14 11-23-2006 09:57 PM
"Non-constant" constant can't be used as template argument Martin Magnusson C++ 2 10-08-2004 08:41 AM
Understanding How To Use #ifdef Constant #define Constant Sequence In Multible Files Christopher M. Lusardi C++ 1 09-02-2004 07:43 AM



Advertisments