good code to return const reference to function local object?

This is a discussion on good code to return const reference to function local object? within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, 1. Returning non-const reference to function local object is not correct. But is it correct to return const ...

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

    good code to return const reference to function local object?

    Hello everyone,


    1. Returning non-const reference to function local object is not correct. But is it correct to return const reference to function local object?

    2. If in (1), it is correct to return const reference to function local object, the process is a new temporary object is created (based on the function local object) and the const reference is binded to the temporary object, and the life time of the temporary object is extended to the life time of const reference?

    Or no need to create such a temporary object, just let the const reference binded to the function local object itself?


    thanks in advance,
    George

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    1) No. Think for one minute before asking question. Why, do you think, is returning a non-const reference to a local object bad? If you can answer this question, you also know whether returning a const reference is bad.

    2) The past simple and past participle of bind are both "bound".
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

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


    1.

    Quote Originally Posted by CornedBee View Post
    1) No. Think for one minute before asking question. Why, do you think, is returning a non-const reference to a local object bad? If you can answer this question, you also know whether returning a const reference is bad.
    Appreciate for your confirmation. But I think const reference and non-const are different, and we can return a temporary object and let const reference binded to it, why not?

    2.

    Quote Originally Posted by CornedBee View Post
    2) The past simple and past participle of bind are both "bound".
    Thanks for correcting my English. :-)


    regards,
    George

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,452
    But I think const reference and non-const are different, and we can return a temporary object and let const reference binded to it, why not?
    We cannot, because the caller would receive a (const) reference to an object that would no longer exist.
    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
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks for your confirmation, laserlight!


    My question is answered.

    Quote Originally Posted by laserlight View Post
    We cannot, because the caller would receive a (const) reference to an object that would no longer exist.

    regards,
    George

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. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM

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