473,898 Members | 2,803 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 1495
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
1428
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
12366
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
1612
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
1278
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
1301
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
2107
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
6752
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
1643
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
1678
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
11259
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
10857
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10948
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9661
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8035
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
7187
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
5880
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4706
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
2
4295
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.