How come this works?

This is a discussion on How come this works? within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <string> std::string Func() { std::string linha; std::getline(std::cin, linha); return linha; } int main() { std::string linha; ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    204

    How come this works?

    Code:
    #include <iostream>
    #include <string>
    
    std::string Func()
    {
    	std::string linha;
    
    	std::getline(std::cin, linha);
    
    	return linha;
    }
    
    
    int main()
    {
    	std::string linha;
    
    	linha = Func();
    
    	std::cout << linha << std::endl;
    }
    In function Func(), I'm returning a local variable and the program displays the correct result. I can't figure out why...
    Thanks.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I suspect the string class has a copy constructor which dupes the string. Much like returning an integer or what not returns a copy (value actually) when you return one.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,826
    Yeah, it looks normal - you arent returning a reference to the local string after all.
    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

  4. #4
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    That code looks ok to me... i mean it is suppose to return a string...

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    It's the same situation as:
    Code:
    int func1()
    {
       return 1;  //Returning 1 by value
    }
    std::string func2()
    {
       return std::string("Some String");  //Return a copy of std::string("Some String")
    }
    You're just returning a value. The problem is, as laserlight mentioned, when you try returning a reference or pointer:
    Code:
    int& func1()
    {
       int localVariable;
       return localVariable;  //localVariable won't exist after func1() ends
    }
    
    std::string* func2()
    {
       std::string localString("Some String");
       return &localString;  //This address will point to nothing when func2() ends
    }
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can anybody show me why this works??
    By tzuch in forum C Programming
    Replies: 2
    Last Post: 03-29-2008, 09:03 AM
  2. fprintf works in one function but not in the other.
    By smegly in forum C Programming
    Replies: 11
    Last Post: 05-25-2004, 03:30 PM
  3. Works only part of the time?
    By scr0llwheel in forum C++ Programming
    Replies: 1
    Last Post: 01-07-2003, 07:34 PM
  4. Programs works one way, not another. VC++ 6.0 help.
    By Cheeze-It in forum Windows Programming
    Replies: 4
    Last Post: 12-10-2002, 09:29 PM
  5. it never works...
    By Ryce in forum Game Programming
    Replies: 5
    Last Post: 08-30-2001, 07:31 PM

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