Reference to temporary

Discussion in 'C++' started by Andriy Shnyr, Feb 25, 2004.

  1. Andriy Shnyr

    Andriy Shnyr Guest

    Hallo!
    I have the following example:
    class Test{
    public:
    Test(int const level): _level(level), _next(0){}
    void add_next(Test* const next){
    _next = next;
    }
    Test* const& get(int const level){
    if(_next != 0 && _next->_level >= level)
    return _next->get(level);
    else
    return this;
    }
    void print(){
    cout << _level<< endl;
    }
    private:
    Test* _next;
    int const _level;
    };
    Compiling this example with gcc 3.2 results in the following warning
    "Returning reference to temporary"
    line "return this"
    Ok, I understand may be it is enough to return "T* const" not a
    reference, but still I do not understand the reason:-(
    Is is safe to return such a reference?
    Andriy Shnyr, Feb 25, 2004
    #1
    1. Advertising

  2. Andriy Shnyr

    Rolf Magnus Guest

    Andriy Shnyr wrote:

    > Hallo!
    > I have the following example:
    > class Test{
    > public:
    > Test(int const level): _level(level), _next(0){}
    > void add_next(Test* const next){
    > _next = next;
    > }
    > Test* const& get(int const level){
    > if(_next != 0 && _next->_level >= level)
    > return _next->get(level);
    > else
    > return this;
    > }
    > void print(){
    > cout << _level<< endl;
    > }
    > private:
    > Test* _next;
    > int const _level;
    > };
    > Compiling this example with gcc 3.2 results in the following warning
    > "Returning reference to temporary"
    > line "return this"
    > Ok, I understand may be it is enough to return "T* const" not a
    > reference,


    Actually, it would even be enough to return a T*, without the const.

    > but still I do not understand the reason:-(
    > Is is safe to return such a reference?


    'this' is a pointer to the object that the function is currently working
    on. It is usually a hidden parameter to the function, and so it is
    local to the function and doesn't exist after returning from it.
    Therefore, I think it's not safe to return a reference to 'this'.
    Rolf Magnus, Feb 25, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexander Stippler
    Replies:
    2
    Views:
    624
    Alexander Stippler
    Jul 4, 2003
  2. =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunion?=

    The temporary vs non-const reference love story

    =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunion?=, Nov 4, 2003, in forum: C++
    Replies:
    13
    Views:
    578
    Ron Natalie
    Nov 6, 2003
  3. Dave

    Reference to temporary

    Dave, Nov 5, 2003, in forum: C++
    Replies:
    11
    Views:
    611
    Calum
    Nov 7, 2003
  4. John Ky
    Replies:
    9
    Views:
    435
    John Ky
    Feb 23, 2004
  5. Replies:
    7
    Views:
    3,204
    James Kanze
    Feb 12, 2008
Loading...

Share This Page