473,796 Members | 2,625 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

question on constant variables

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

using namespace std;

const static char* STATIC_CONST_NA ME = "Hello";
const char* CONST_NAME = "Hello";

int main(int argc, char *argv[])
{
cout << STATIC_CONST_NA ME << 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 '08 #1
6 1492
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.
Sep 9 '08 #2
va********@atla s.cz wrote:
...
const static char* STATIC_CONST_NA ME = "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_NA ME = "Hello";
const char* const CONST_NAME = "Hello";

(note the extra inner 'const's).

--
Best regards,
Andrey Tarasevich
Sep 9 '08 #3
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_NA ME = "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
Sep 9 '08 #4
On 9 Sep, 21:07, Andrey Tarasevich <andreytarasev. ..@hotmail.com>
wrote:
vaclavp...@atla s.cz wrote:
...
const static char* STATIC_CONST_NA ME = "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_NA ME = "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 '08 #5
gw****@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_NA ME = "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
Sep 9 '08 #6
On Sep 9, 9:38 pm, vaclavp...@atla s.cz wrote:
I have a question on constant variables.
/////////////////////////////////////////////
//
#include <cstdlib>
#include <iostream>
using namespace std;
const static char* STATIC_CONST_NA ME = "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_NA ME << 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:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 10 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1426
by: Jan Bernatik | last post by:
Hi I have a function called from another one, which runs in loop, so that is it called very often something like this: void my_function(int x) { int temp; temp = x * some_const;
6
12363
by: Prasad | last post by:
Where are the const variables actually stored in memory. I mean If they are stored in data segment(external & static variables)or stack segment(local) how the compiler knows that it is read only memory.
6
1610
by: Alfonso Morra | last post by:
Hi, I have a function declared thus: int foo( int, int ); In the definition, I want to have a variable that is expensive to create. I want to be able to save the state of this variable accross calls - so I declare the var as a static - nothing new here.
3
1273
by: C# Learner | last post by:
Should constant class member variables be held like this: public class Foo { public const int ConstVar = 100; } Or like this: public class Foo
6
1296
by: Simon Harvey | last post by:
Hi, Is there any benefit to declaring variables as constants other than not being able to overwrite them by mistake? Thanks Simon
5
2101
by: Zach | last post by:
When it is being said that, "value types are created on the stack or inline as part of an object". If a value type is created in an object, and that object is being called, the value type in that object, is still created on the stack, I would say, so I don't understand this inline business. Apart from the fact that it is my understanding that "inline" as it exists in C++ doesn't exist in C#. Could someone please shed some light on this...
4
6745
by: Hans | last post by:
Hi, I want to define a couple of constant strings, like in C: #define mystring "This is my string" or using a const char construction. Is this really not possible in Python? Hans
6
1633
by: lazy | last post by:
hi, I have some constants defined in a php script say config.php. I want to use the variables there defined in other scripts. couple of questions regd that: 1. Is there an alternative to including config.php and declaring the variables that will be used as global. This seems very inefficient. 2.Moreover these variables are constants, is there a way to make the variables unmodifiable in config.php so that scripts that use them ,
8
1670
by: fabian.lim | last post by:
Hi, I have a question on constant variables. In the following code snippet, I have a function assign() that takes in an iterator to the private variable v, the number of stuff to assign (int n), and the information to assign (a const pointer to a class object Vector<TYPE>. According to the arrow below, I put a const keyword in the function. To my knowledge, this means that all private variables in this object
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9531
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10459
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10018
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7553
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6795
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5578
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4120
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.