PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > String constant
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
String constant

Réponse
 
LinkBack Outils de la discussion
Vieux 06/06/2008, 14h53   #1
tech
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut String constant

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
  Réponse avec citation
Vieux 06/06/2008, 15h02   #2
anon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: String constant

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";
  Réponse avec citation
Vieux 06/06/2008, 17h44   #3
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: String constant

On Jun 6, 3:53 pm, tech <naumansulai...@googlemail.com> 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:james.kanze@gmail.com
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
  Réponse avec citation
Vieux 06/06/2008, 17h44   #4
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: String constant

On Jun 6, 4:02 pm, anon <a...@no.no> 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:james.kanze@gmail.com
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

  Réponse avec citation
Vieux 06/06/2008, 18h19   #5
Daniel Pitts
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: String constant

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/>
  Réponse avec citation
Vieux 07/06/2008, 22h27   #6
Ian Collins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: String constant

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.
  Réponse avec citation
Vieux 08/06/2008, 09h36   #7
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: String constant

On Jun 7, 11:27 pm, Ian Collins <ian-n...@hotmail.com> 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:james.kanze@gmail.com
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
  Réponse avec citation
Vieux 08/06/2008, 10h36   #8
Ian Collins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: String constant

James Kanze wrote:
> On Jun 7, 11:27 pm, Ian Collins <ian-n...@hotmail.com> 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.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h38.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13715 seconds with 16 queries