Thread: Beginner question: string("Chimera")

  1. #1
    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
    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
    28,413
    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));
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,149
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    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, 06: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, 08:39 PM