question on constant variables

V

vaclavpich

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.
 
M

mqrk

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.
 
A

Andrey Tarasevich

...
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).
 
A

Andrey Tarasevich

Andrey said:
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.
 
G

gw7rib

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?
 
A

Andrey Tarasevich

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.
 
J

James Kanze

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++.)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top