Beginner question: string("Chimera")

This is a discussion on Beginner question: string("Chimera") within the C++ Programming forums, part of the General Programming Boards category; Hello all, I am just starting out on C++ having spent some time learning C. I have come across the ...

  1. #1
    DL1
    DL1 is offline
    Registered User
    Join Date
    Jun 2008
    Location
    Somewhere in Europe
    Posts
    99

    Beginner question: string("Chimera")

    Hello all,

    I am just starting out on C++ having spent some time learning C.

    I have come across the following declarations in Chapter 6 of Lippman (3rd edition):

    Code:
    extern int get_word_count(string file_name);
    vector<string> svec(get_word_count(string("Chimera")));
    I am rather puzzled by the expression string("Chimera").

    Is this casting a string literal into a string type or something? I haven't been able to find any reference to such expressions anywhere. The closest I have found is declarations of the type string string_name("Chimera"), but in the example above there is no identifier.

    Also, what value would in such an expression - or the parameter string file_name - have (in C it would be a pointer to char; is it the same in C++, or is the value of an object simply the object itself?)?

    Many thanks for any help.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It creates a temporary string object using the constructor that takes const char*, to pass it to a function.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    DL1
    DL1 is offline
    Registered User
    Join Date
    Jun 2008
    Location
    Somewhere in Europe
    Posts
    99
    Thanks. Can you do that other (all?) constructors? E.g. would the following work?

    Code:
    string s1("Hello!");
    extern int get_word_count(string file_name);
    vector<string> svec(get_word_count(string(s1)));

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    E.g. would the following work?
    Before asking, try it yourself.

    That said, yes, it would work, but you might as well write:
    Code:
    vector<string> svec(get_word_count(s1));
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    That would use the copy constructor. In this case there is no actual need for a copy, so you might just use get_word_count(s1).

    And yes, it would work for all kinds of constructors:

    Code:
        string s("hello");
        get_word_count(string()); //default constructor (empty string)
        get_word_count(string(s)); //using copy constructor
        get_word_count(string("Hello")); //from const char*
        get_word_count(string("hello", 3)); //first three characters of "hello"
        get_word_count(string(5, 'a')); //= "aaaaa"
        get_word_count(string(s.begin(), s.begin() + 2)); //from range = "he"
        get_word_count(string(s, 1, 3)); //substring of s = "elo"
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,023
    Quote Originally Posted by DL1 View Post
    Thanks. Can you do that other (all?) constructors? E.g. would the following work?

    Code:
    string s1("Hello!");
    extern int get_word_count(string file_name);
    vector<string> svec(get_word_count(string(s1)));
    Yes, that should work.

    You can do it with all constructors, even primatives. For primatives it acts just like a C style cast.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    It simply calls a constructor for the specified type.
    It can be done with any type, providing it has an appropriate constructor.
    Since you do not assign it to anything, the object becomes temporary - it usually dies after the line is question.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    DL1
    DL1 is offline
    Registered User
    Join Date
    Jun 2008
    Location
    Somewhere in Europe
    Posts
    99
    Thanks again.

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    And one last option that isn't mentioned:

    Code:
    extern int get_word_count(string file_name);
    vector<string> svec(get_word_count("Chimera"));
    When you pass const char* to a function that expects a string, the string file_name will be constructed using implicitly the suitable constructor.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner: Linked List question
    By WeatherMan in forum C++ Programming
    Replies: 2
    Last Post: 04-03-2008, 07:16 AM
  2. Quick IF statement question (beginner)
    By jim.rattlehead in forum C Programming
    Replies: 23
    Last Post: 11-29-2007, 05:51 AM
  3. beginner question
    By Barrot in forum C++ Programming
    Replies: 4
    Last Post: 08-19-2005, 02:17 PM
  4. Question About External Files (Beginner)
    By jamez05 in forum C Programming
    Replies: 0
    Last Post: 08-11-2005, 07:05 AM
  5. Beginner on Win32 apps, lame question.
    By Templario in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 07:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21