473,386 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

return reference to local variable, good code?

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

Expand|Select|Wrap|Line Numbers
  1. class Foo {
  2.  
  3. };
  4.  
  5. Foo& func()
  6. {
  7. Foo foo;
  8. return foo;
  9. }
  10.  
  11. int main()
  12. {
  13. Foo& foo = func();
  14.  
  15. return 0;
  16. }
  17.  
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
Dec 16 '07 #1
7 3960
On 12月16日, 下午1时49分, George2 <george4acade...@yahoo.comwrote:
Hello everyone,

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

Expand|Select|Wrap|Line Numbers
  1. class Foo {
  2. };
  3. Foo& func()
  4. {
  5.         Foo foo;
  6.         return foo;
  7. }
  8. int main()
  9. {
  10.         Foo& foo = func();
  11.         return 0;}
  12.  

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
I'm afraid this code can not be compiled. To return a reference of
local non-static object is not allowed.
Dec 16 '07 #2
Alf P. Steinbach wrote:
And I'm sorry this is EXTREMELY OFF-TOPIC, but perhaps someone knows how
this can be?

It seems the connection problems are protocol-dependent.

Although I can't read my mail (GMail), I can read the New York Times,
but pinging that server fails:
Is your PC infected by windows ??

--
Ian Collins.
Dec 16 '07 #3
On Dec 16, 7:11 am, "Alf P. Steinbach" <al...@start.nowrote:
* yanlinlin:
On 12鏈16鏃, 涓嬪崍1鏃49鍒, George2 <george4acade...@yahoo.comwrote:
I am reading some code from other people, there are some code like
this,
Expand|Select|Wrap|Line Numbers
  1. class Foo {
  2. };
Expand|Select|Wrap|Line Numbers
  1.         
  2.                         
  3.                 Foo& func()
  4. {
  5.         Foo foo;
  6.         return foo;
  7. }
  •  
  •         
  •                         
  •                 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.
    I'm afraid this code can not be compiled. To return a
    reference of local non-static object is not allowed.
    It's allowed and it should compile.
    A compiler that doesn't allow it is non-conforming.
    However, using that result incurs Undefined Behavior.
    I think that if the compiler can prouve that the result is used
    (and it is here), then it is allowed to reject the code. (The
    case here is an interesting one, in fact---I'm assuming that the
    same rules hold for references as for pointers, and that even
    copying an invalid reference is undefined behavior. I've not
    looked it up, however, so caveat empor.)
    PS: Could you, or anyone, confirm whether you see this
    posting? If you don't see it, simply do nothing.
    I've seen it. (Rather obviously.) I even figured out something
    on-topic to add so that I wouldn't be off topic in
    responding:-).

    --
    James Kanze (GABI Software) email:ja*********@gmail.com
    Conseils en informatique orient茅e objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place S茅mard, 78210 St.-Cyr-l'脡cole, France, +33 (0)1 30 23 00 34
    Dec 16 '07 #4
    It's allowed and it should compile.
    >
    A compiler that doesn't allow it is non-conforming.

    However, using that result incurs Undefined Behavior.

    Cheers, & hth.,

    - Alf
    Yes, I misunderstood that.
    It can compile successfully, but with a warning. And the option "treat
    warning as error" misguided me about it. :P
    Dec 16 '07 #5
    On Dec 16, 13:39, James Kanze <james.ka...@gmail.comwrote:
    On Dec 16, 7:11 am, "Alf P. Steinbach" <al...@start.nowrote:
    * yanlinlin:
    On 12月16日, 下午1时49分, George2 <george4acade...@yahoo.comwrote:
    >I am reading some code from other people, there are some code like
    >this,
    >
    Expand|Select|Wrap|Line Numbers
    1.  >class Foo {
    2.  >};
    Expand|Select|Wrap|Line Numbers
    1.         
    2.                         
    3.                  >Foo& func()
    4.  >{
    5.  >        Foo foo;
    6.  >        return foo;
    7.  >}
  •  
  •         
  •                         
  •                  >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.
    I'm afraid this code can not be compiled. To return a
    reference of local non-static object is not allowed.
    It's allowed and it should compile.
    A compiler that doesn't allow it is non-conforming.
    However, using that result incurs Undefined Behavior.

    I think that if the compiler can prouve that the result is used
    (and it is here), then it is allowed to reject the code. (The
    case here is an interesting one, in fact---I'm assuming that the
    same rules hold for references as for pointers, and that even
    copying an invalid reference is undefined behavior. I've not
    looked it up, however, so caveat empor.)

    How can compiler prove that the result is used? How can he prove i'm
    not writing a thread that's supposed to be externally terminated in
    the very same case as when the function happens to return a reference-
    to-local, the reference later explicitely being used but unreachable
    in practice? :-)
    Dec 17 '07 #6
    On 16 Dec, 06:09, "Alf P. Steinbach" <al...@start.nowrote:
    * George2:


    Hello everyone,
    I am reading some code from other people, there are some code like
    this,
    Expand|Select|Wrap|Line Numbers
    1.  class Foo {
    Expand|Select|Wrap|Line Numbers
    1.         
    2.                  };
  •  
  •         
  •                  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.

    It's Undefined Behavior if the function result is ever used.

    Btw., could you or anyone else please confirm that you can see this posting.

    It seems that all internet connections out of Norway are down at the
    moment, at least via my ISP (e.g. I can't reach Microsoft's servers, nor
    Google), yet articles keep dropping in in international newsgroups.

    Of course, if you don't see this posting, simply don't respond.
    i can c u. uk
    Dec 17 '07 #7
    On Dec 17, 11:56 am, Pavel Shved <Pavel.Sh...@gmail.comwrote:
    On Dec 16, 13:39, James Kanze <james.ka...@gmail.comwrote:
    On Dec 16, 7:11 am, "Alf P. Steinbach" <al...@start.nowrote:
    * yanlinlin:
    On 12鏈16鏃, 涓嬪崍1鏃49鍒, George2 <george4acade...@yahoo.comwrote:
    I am reading some code from other people, there are some code like
    this,
    Expand|Select|Wrap|Line Numbers
    1.  class Foo {
    2.  };
    Expand|Select|Wrap|Line Numbers
    1.  
    2.         
    3.                         
    4.                         
    5.                  Foo& func()
    6.  {
    7.          Foo foo;
    8.          return foo;
    9.  }
    10.  
    11.  
    12.         
    13.                         
    14.                         
    15.                  int main()
    16.  {
    17.          Foo& foo = func();
    18.          return 0;}
    19.  
    20.  
    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.
    I'm afraid this code can not be compiled. To return a
    reference of local non-static object is not allowed.
    It's allowed and it should compile.
    A compiler that doesn't allow it is non-conforming.
    However, using that result incurs Undefined Behavior.
    I think that if the compiler can prouve that the result is
    used (and it is here), then it is allowed to reject the
    code. (The case here is an interesting one, in fact---I'm
    assuming that the same rules hold for references as for
    pointers, and that even copying an invalid reference is
    undefined behavior. I've not looked it up, however, so
    caveat empor.)
    How can compiler prove that the result is used?
    By flow analysis.
    How can he prove i'm not writing a thread that's supposed to
    be externally terminated in the very same case as when the
    function happens to return a reference- to-local, the
    reference later explicitely being used but unreachable in
    practice? :-)
    That's undefined behavior as well, as far as the standard goes,
    so the compiler doesn't have to take it into account. (In fact,
    of course, it's also impossible to do on most modern systems;
    another process can't really look at the individual instructions
    you're executing.)

    --
    James Kanze (GABI Software) email:ja*********@gmail.com
    Conseils en informatique orient茅e objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place S茅mard, 78210 St.-Cyr-l'脡cole, France, +33 (0)1 30 23 00 34

    Dec 17 '07 #8

    This thread has been closed and replies have been disabled. Please start a new discussion.

    Similar topics

    2
    by: Kench | last post by:
    I was curious and playing with pointers and references to see what's different between them. Other than the obvious ones involving C++ syntax & things like references cannot be modified with...
    25
    by: cppaddict | last post by:
    I'd like to know what goes on under the hood when methods return objects. Eg, I have a simple Point class with two members _x and _y. It's constructor, copy constructor, assignment operator and...
    14
    by: Gama Franco | last post by:
    Hi, I'm designing an interface for a shared library, and I would like to know if there is a standard about how to return an object to the user. I will use exceptions to report errors, so there...
    5
    by: Neal Coombes | last post by:
    Posted to comp.lang.c++.moderated with little response. Hoping for better from the unmoderated groups: -------- Original Message -------- Subject: Return appropriately by value, (smart)...
    23
    by: Nascimento | last post by:
    Hello, How to I do to return a string as a result of a function. I wrote the following function: char prt_tralha(int num) { int i; char tralha;
    2
    by: Jess | last post by:
    Hello, I understand that if a function "f" has a local variable "a", and after it returns, "a" vanishes. If "f" returns "a" as a result, then I noticed the following: 1. if the return type is...
    2
    by: George2 | last post by:
    Hello everyone, I am reading some code from other people, there are some code like this, class Foo { };
    2
    by: zheng4t | last post by:
    I am learning C++. I found the following code in the book The C++ Programming Language by Bjarne Stroustrup. struct Pair { string name; double val; }; vector<Pairpairs; double& value(const...
    275
    by: Astley Le Jasper | last post by:
    Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
    0
    by: taylorcarr | last post by:
    A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
    0
    by: Charles Arthur | last post by:
    How do i turn on java script on a villaon, callus and itel keypad mobile phone
    0
    BarryA
    by: BarryA | last post by:
    What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
    1
    by: nemocccc | last post by:
    hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
    0
    by: Hystou | last post by:
    There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
    0
    marktang
    by: marktang | last post by:
    ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
    0
    by: Hystou | last post by:
    Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
    0
    Oralloy
    by: Oralloy | last post by:
    Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
    0
    jinu1996
    by: jinu1996 | last post by:
    In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

    By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

    To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.