Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > String constant

Reply
Thread Tools

String constant

 
 
tech
Guest
Posts: n/a
 
      06-06-2008
Hi, I need to define some strings in a header file, they are to be
const
Whats the best to choose from below;

const std::string s = "Hello";
const char* s = "Hello";
char* s = "Hello";

Thanks
 
Reply With Quote
 
 
 
 
anon
Guest
Posts: n/a
 
      06-06-2008
tech wrote:
> Hi, I need to define some strings in a header file, they are to be
> const
> Whats the best to choose from below;
>
> const std::string s = "Hello";
> const char* s = "Hello";
> char* s = "Hello";
>
> Thanks


I am doing one of two:
const std::string s1 = "Hello";
const char* const s2 = "Hello";
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      06-06-2008
On Jun 6, 3:53 pm, tech <(E-Mail Removed)> wrote:
> Hi, I need to define some strings in a header file, they are
> to be const Whats the best to choose from below;


> const std::string s = "Hello";
> const char* s = "Hello";
> char* s = "Hello";


You can't really put the latter two in a header file without
getting duplicate definitions, since they aren't const. But you
ignore one of the most frequent alternatives:

char const s[] = "Hello" ;

It has the advantage of allowing static initialization, but does
mean that you'll probably end up having to construct an
std::string each time you use it, which tends to offset the
initial advantage of static initialization; but the static
initialization still has the advantage of avoiding order of
initialization problems, and the compiler can also ignore the
definition if you don't actually use the variable in a module.

--
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
 
James Kanze
Guest
Posts: n/a
 
      06-06-2008
On Jun 6, 4:02 pm, anon <(E-Mail Removed)> wrote:
> tech wrote:
> > Hi, I need to define some strings in a header file, they are
> > to be const Whats the best to choose from below;


> > const std::string s = "Hello";
> > const char* s = "Hello";
> > char* s = "Hello";


> I am doing one of two:
> const std::string s1 = "Hello";
> const char* const s2 = "Hello";


Why the pointer?

--
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
 
Daniel Pitts
Guest
Posts: n/a
 
      06-06-2008
anon wrote:
> tech wrote:
>> Hi, I need to define some strings in a header file, they are to be
>> const
>> Whats the best to choose from below;
>>
>> const std::string s = "Hello";
>> const char* s = "Hello";
>> char* s = "Hello";
>>
>> Thanks

>
> I am doing one of two:
> const std::string s1 = "Hello";
> const char* const s2 = "Hello";

Right, don't forget both the const's if you use char *
You want a constant pointer to a constant value.

Depending on your typical use, I would suggest const std::string, since
string adds so much utility.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      06-07-2008
tech wrote:
> Hi, I need to define some strings in a header file, they are to be
> const
> Whats the best to choose from below;
>
> const std::string s = "Hello";


This is the only version here than can go in a header.

> const char* s = "Hello";


This has to be

const char* const s = "Hello";

Which you use depends on how you use them. If you wish to add to one
later (say it's a root file path), use std::string. If they are
discrete tokens, const char* const can be used. But do bear in mind a
std::string will be constructed each tome you pass one to a function
with a const std::string reference parameter.


--
Ian Collins.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      06-08-2008
On Jun 7, 11:27 pm, Ian Collins <(E-Mail Removed)> wrote:
> tech wrote:
> > I need to define some strings in a header file, they are to be
> > const
> > Whats the best to choose from below;


> > const std::string s = "Hello";


> This is the only version here than can go in a header.


Of the forms he proposes.

> > const char* s = "Hello";


> This has to be


> const char* const s = "Hello";


Again: what's wrong with:

char cosnt s[] = "Hello" ;

? Why do you need the extra pointer.

> Which you use depends on how you use them. If you wish to add
> to one later (say it's a root file path), use std::string. If
> they are discrete tokens, const char* const can be used. But
> do bear in mind a std::string will be constructed each time
> you pass one to a function with a const std::string reference
> parameter.


On the other hand, the std::string form may suffer from order of
initialization issues.

--
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
 
Ian Collins
Guest
Posts: n/a
 
      06-08-2008
James Kanze wrote:
> On Jun 7, 11:27 pm, Ian Collins <(E-Mail Removed)> wrote:
>> tech wrote:

>
>>> const char* s = "Hello";

>
>> This has to be

>
>> const char* const s = "Hello";

>
> Again: what's wrong with:
>
> char cosnt s[] = "Hello" ;
>

It won't compile?

> ? Why do you need the extra pointer.
>

No particular reason, just habit. I'm just used to thinking of a
constant pointer to a string literal. A string literal has to have an
address, so where's the extra pointer?

--
Ian Collins.
 
Reply With Quote
 
Frank Birbacher
Guest
Posts: n/a
 
      06-08-2008
Hi!

Ian Collins schrieb:
> No particular reason, just habit. I'm just used to thinking of a
> constant pointer to a string literal. A string literal has to have an
> address, so where's the extra pointer?


The pointer itself also has an address. It's one address more than the
array solution has. The array solution saves sizeof(char*) bytes (or
whatever) in the resulting object file (or it is optimized away).

Frank
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      06-08-2008
Frank Birbacher wrote:
> Hi!
>
> Ian Collins schrieb:
>> No particular reason, just habit. I'm just used to thinking of a
>> constant pointer to a string literal. A string literal has to have an
>> address, so where's the extra pointer?

>
> The pointer itself also has an address. It's one address more than the
> array solution has. The array solution saves sizeof(char*) bytes (or
> whatever) in the resulting object file (or it is optimized away).
>

That was my point, it will more then likely be optimised away.

--
Ian Collins.
 
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
concatenate a constant to constant string using macros sinbad C Programming 7 06-19-2008 05:28 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