Thread: string reference

  1. #1
    Registered User Kirdra's Avatar
    Join Date
    Aug 2002
    Posts
    105

    string reference

    Let's say I have a function that takes a reference to a string, is it safe to pass a c style string to this function? Or should I use a local copy so the constructor does it's job? I tested this and it works it just seems strange that I can pass a c string to a function that wants a const std::string&.

    Thanks.
    Last edited by Kirdra; 02-26-2011 at 01:04 AM.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Kirdra View Post
    Let's say I have a function that takes a reference to a string, is it safe to pass a c style string to this function? Or should I use a local copy so the constructor does it's job? I tested this and it works it just seems strange that I can pass a c string to a function that wants a const std::string&.

    Thanks.
    It works because there is an implicit conversion from c-string to std::string available, and the function takes a const reference. Had the reference not been const it would not have worked. A temporary std::string is constructed, a reference to this is passed to the function, then the temporary is destructed.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Quote Originally Posted by brewbuck View Post
    It works because there is an implicit conversion from c-string to std::string available, and the function takes a const reference. Had the reference not been const it would not have worked. A temporary std::string is constructed, a reference to this is passed to the function, then the temporary is destructed.
    Do you know why the reference needs to be const? If it's a temporary that will only be available for that function, what's the harm in modifying it?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Mozza314 View Post
    If it's a temporary that will only be available for that function, what's the harm in modifying it?
    Expectations of programmers calling the function. In an argument is passed by non-const reference, the common expectation is that any changes made in the function will be visible to the caller.

    Let's say we have this;
    Code:
    #include <string>
    #include <iostream>
    
    void set_bye(std::string &s)
    {
        s = "Bye";
    }
    
    int main()
    {
          char x[] = "Hello";
          std:string y("Hello");
    
          set_bye(x);
          set_bye(y);
    
          std::cout << x << '\n';
          std::cout << y << '\n';
    }
    So, depending on what is passed, the value passed may change in some instances but not in others.

    Implicit conversions make such cases problematical.

    As you say, however, there are cases where doing such things is not harmful. There is some consideration in C++0x to relaxing such restrictions.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by grumpy View Post
    As you say, however, there are cases where doing such things is not harmful. There is some consideration in C++0x to relaxing such restrictions.
    I'm not much in C++0x, but I don't like the idea of allowing (implicit and explicit) convertion to non-const reference. It's just confusing when you see code passing some values without caring about the results. When you see a local object which is ignored later, it is easily recognisable that the programmer did it purposedly.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well. C++0x introduces r-value references, actually, which would allow you to pass a char array to a function accepting a r-value reference for a std::string.
    The idea behind it is that r-value references will only bind to temporaries, so we're essentially saying: "This is a temporary, so we don't care about its value", and so the function can steal the temporary's resources instead of copying them.
    That is the only relaxing of restrictions I know of.
    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.

  7. #7
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Sorry for this stupid question..but how is char x[] a temporary?
    Borland says it is ..but it is declared in main..and how does its value still remain "Hello"
    Thanks
    You ended that sentence with a preposition...Bastard!

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    char x[] isn't a temporary, but when you pass it to a function which does not accept a char[], then the compiler will perform an implicit conversion (if possible), and this new variable that it creates will be a temporary. So in this example, the std::string created by an implicit conversion from the char array is a temporary.
    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.

  9. #9
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    oh thanks Elysia..i get it
    You ended that sentence with a preposition...Bastard!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Free program I'm sharing: ConvertEnumToStrings
    By Programmer_P in forum Projects and Job Recruitment
    Replies: 101
    Last Post: 07-18-2010, 12:55 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  4. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM