|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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"; |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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/> |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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. |
|
![]() |
| Outils de la discussion | |
|
|