References again

From: JKop (NULL_at_NULL.NULL)
Date: 06/18/04


Date: Fri, 18 Jun 2004 16:17:57 GMT

Take the following:

int GiveMeInt(void)
{
        return 734;
}

int main(void)
{
        const int& plain = GiveMeInt();
        
        //Time goes by

            TakeConstIntPointer(&plain);
}

I know that there's nothing wrong with the above because the reference has
extended the life of the variable.

But take the following:

int& GiveMeIntRef(void)
{
        int cow = 343;
        
        return cow;
}

int main(void)
{
        const int& ref = GiveMeIntRef();

            //Time goes by

            TakeConstIntPointer(&ref);
}

Has the lifetime been extended in the above?? Or am I accessing a variable
that no longer exists? If so, should I take the following as a general
guidline: Only return a reference from a function if it was declared via
new. ?

-JKop



Relevant Pages

  • Re: [PATCH] Make futex waiters take an mm or inode reference
    ... Make futex waiters take an mm or inode reference ... Rusty Russell wrote: ... +static inline void drop_key_refs ... -static int futex_wake ...
    (Linux-Kernel)
  • Re: dynamic type checking - a pauline conversion?
    ... it depends on the type of reference to the object. ... In Smalltalk the reference doesn't have a type but the instance does ... and C++ can not guarantee type correctness at compile ...
    (comp.object)
  • Re: Simplicity
    ... > ByRef (by reference) ... > Dim k as Zone ... property int Value; ... Microsoft C/C++ Optimizing Compiler Version 14.00.40809 ...
    (comp.lang.cpp)
  • [CFT]: ipfw named tables / different tabletypes
    ... This patch adds ability to name tables / reference them by name. ... +struct table_config { ... int is_all; ...
    (freebsd-net)
  • Re: hash two keys to one index
    ... What goes into the map are pairs of (reference to key, ... When I insert an object into the hash table, I pass in ... void insert(Object obj, int hash) throws HashTableFull ... int probe = 0; ...
    (comp.lang.java.programmer)