returning a reference to string

This is a discussion on returning a reference to string within the C++ Programming forums, part of the General Programming Boards category; Hi, How do I properly put this to code? I am trying to return a string (name) from a function ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    Jacksonville, AR
    Posts
    91

    returning a reference to string

    Hi,

    How do I properly put this to code? I am trying to return a string (name) from a function using reference.

    Code:
    std::string& name()
    {
       cout << "Enter your name: " << endl;
       getline(cin, name);
    }
    I hope you get the idea. Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,274
    You need to pass in the string by reference, but you probably should either rename the function or use a different name for the string reference parameter. Of course, once you pass in the string by reference, there is not much point in returning it at all.
    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

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    14
    I think there are some cases that benefit from returning a reference. For example, I have a function to convert something to a string, and use the converted string in a string expression. I can pass in a string reference, store the converted string in it, and return a reference to it. This way, I can avoid a string copy operation. Here is some abstract code to explain what I mean:
    Code:
    string & int2str(int i, string & str){
      // convert i into str
      return str;
    }
    
    int main(){
    
      ...
      string tmp;
      string a = "abc" + int2str(20, tmp);
      ...
    }

    Quote Originally Posted by laserlight View Post
    You need to pass in the string by reference, but you probably should either rename the function or use a different name for the string reference parameter. Of course, once you pass in the string by reference, there is not much point in returning it at all.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,274
    Quote Originally Posted by plutino
    I think there are some cases that benefit from returning a reference.
    Yes, but I do not think that this is one of them.

    Quote Originally Posted by plutino
    This way, I can avoid a string copy operation.
    Depending on your point of view, you either cannot avoid the copy (since you are initialising a new string with a temporary) or can avoid the copy either way, e.g.,
    Code:
    string tmp;
    int2str(20, tmp);
    string a = "abc" + tmp;
    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
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    The compiler can also optimize away functions returning copies of variables, if they are not too complicated.
    But you cannot return a reference to a local variable!
    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.

  6. #6
    Registered User
    Join Date
    Dec 2008
    Posts
    14
    You are right. I guess the only benefit (depending on your point of view) is to make the code more compact.

    Quote Originally Posted by laserlight View Post
    Yes, but I do not think that this is one of them.


    Depending on your point of view, you either cannot avoid the copy (since you are initialising a new string with a temporary) or can avoid the copy either way, e.g.,
    Code:
    string tmp;
    int2str(20, tmp);
    string a = "abc" + tmp;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 05:30 PM
  2. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 10:37 PM

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