return reference to local variable, good code?

This is a discussion on return reference to local variable, good code? within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, I am reading some code from other people, there are some code like this, Code: class Foo { ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    return reference to local variable, good code?

    Hello everyone,


    I am reading some code from other people, there are some code like this,

    Code:
    class Foo {
    
    };
    
    Foo& func()
    {
    	Foo foo;
    	return foo;
    }
    
    int main()
    {
    	Foo& foo = func();
    
    	return 0;
    }
    I want to confirm with you that it is not good code, since we return a reference to local object instance, right? Even if the code has expected function currently.


    thanks in advance,
    George

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,765
    Very bad code. It should not work correctly since the variable you pass a reference to will be destructed once func exits - thus main works on a destructed object - and object that does not exist.
    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.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It'll probably work ok in this case, but as soon as you call another function, it's going to use the same space on the stack as the local variable, which of course overwrites the data.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    You're not trying to use the dangling reference so you most likely wont have any problems with that code. However, if you had some members in foo and were later trying to output them via cout from main, then you will almost certainly get nothing but garbage, at best.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  3. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  4. Problem with OpenGL tutorial
    By 2Biaz in forum Windows Programming
    Replies: 18
    Last Post: 09-16-2004, 11:02 AM

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