String constant

Discussion in 'C++' started by tech, Jun 6, 2008.

  1. tech

    tech Guest

    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
     
    tech, Jun 6, 2008
    #1
    1. Advertising

  2. tech

    anon Guest

    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";
     
    anon, Jun 6, 2008
    #2
    1. Advertising

  3. tech

    James Kanze Guest

    On Jun 6, 3:53 pm, 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";


    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:
    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
     
    James Kanze, Jun 6, 2008
    #3
  4. tech

    James Kanze Guest

    On Jun 6, 4:02 pm, 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";


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


    Why the pointer?

    --
    James Kanze (GABI Software) email:
    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
     
    James Kanze, Jun 6, 2008
    #4
  5. tech

    Daniel Pitts Guest

    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/>
     
    Daniel Pitts, Jun 6, 2008
    #5
  6. tech

    Ian Collins Guest

    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.
     
    Ian Collins, Jun 7, 2008
    #6
  7. tech

    James Kanze Guest

    On Jun 7, 11:27 pm, Ian Collins <> 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:
    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
     
    James Kanze, Jun 8, 2008
    #7
  8. tech

    Ian Collins Guest

    James Kanze wrote:
    > On Jun 7, 11:27 pm, Ian Collins <> 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.
     
    Ian Collins, Jun 8, 2008
    #8
  9. 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
     
    Frank Birbacher, Jun 8, 2008
    #9
  10. tech

    Ian Collins Guest

    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.
     
    Ian Collins, Jun 8, 2008
    #10
  11. tech

    James Kanze Guest

    On Jun 8, 12:32 pm, Frank Birbacher <> wrote:

    > 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).


    One less level of indirection can often help the optimizer.

    But more generally, conceptually, I think of this as giving a
    symbolic name to a string literal. The same as something like:
    int const toto = 43 ;
    gives a symbolic name to the int const. For the most part, in
    fact, coming from C, I think of such const variables as
    replacing a hash define. So instead of:
    #define TOTO 43
    #define TITI "Hello!"
    I have:
    int const toto = 43 ;
    char const titi[] = "Hello!" ;
    With exactly the same types I had with the #define.

    Fundamentally, of course, one might question the wisdom of
    defining any form of string constant in a header file. Unlike
    the case of integral constants, and to a lesser degree, at least
    on some architectures, floating point constants, there's not
    much the compiler can do with the initialization value. So
    something like:
    extern char const titi[] ;
    seems generally more appropriate in the header. With, in some
    source file:
    extern char const titi[] = "Hello" ;
    (And I would usually prefer char const[] for this, rather than
    std::string, to be clear of any order of initialization issues.)

    --
    James Kanze (GABI Software) email:
    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
     
    James Kanze, Jun 9, 2008
    #11
  12. tech

    James Kanze Guest

    On Jun 8, 12:35 pm, Ian Collins <> wrote:
    > Frank Birbacher wrote:
    > > 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.


    But the extra pointer reduces the probability that the string
    literal itself will be optimized away, since it is actually
    "used". (Of course, a good compiler should be able to follow
    the link---having suppressed the pointer, the string literal is
    no longer used. Usual practice for automatic variables, but I
    don't know whether this is frequently done for static variables
    or not.

    --
    James Kanze (GABI Software) email:
    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
     
    James Kanze, Jun 9, 2008
    #12
  13. tech

    anon Guest

    James Kanze wrote:
    > On Jun 6, 4:02 pm, 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";

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

    >
    > Why the pointer?


    Why not?


    PS Sorry for very slow response :(
     
    anon, Jun 12, 2008
    #13
  14. tech

    anon Guest

    James Kanze wrote:
    > On Jun 6, 4:02 pm, 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";

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

    >
    > Why the pointer?


    I just read your other response.
    Some functions/methods are expecting const char* as parameter. I know I
    could do s1.c_str(), but I could pass directly const char*
    I guess just a matter of preferences ;)
     
    anon, Jun 12, 2008
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christopher M. Lusardi
    Replies:
    1
    Views:
    4,094
  2. Martin Magnusson
    Replies:
    2
    Views:
    508
    John Harrison
    Oct 8, 2004
  3. Tor Erik Soenvisen
    Replies:
    14
    Views:
    564
    Tim Roberts
    Nov 23, 2006
  4. Replies:
    4
    Views:
    339
    Keith Thompson
    Dec 14, 2006
  5. sinbad
    Replies:
    7
    Views:
    659
    sinbad
    Jun 19, 2008
Loading...

Share This Page