As has already been pointed out you should not return a reference or a pointer to a variable created locally in the function. Instead you'd have to create the variable dynamically in the function.
int* myFunc() ( int* i = new int; *i = 10; return i;}int& myFunc2() { int* i = new int; *i = 10; return *i;)
I have to admit though, that I'm not 100% sure that the second one is safe, as I'm not entirely certain what happens when a reference to a variable on the heap goes out of scope. If only the reference goes out of scope, and not the 'real' variable, version 2 is leaky. Maybe someone else can shed some more light on this?
-Neophyte
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GED d- s:+ a- C++$ UL++ P++ L++ E W+ N+ o K? w !O M--(+) V-- PS+
PE Y+ PGP t-- 5++ X+ R(+) rv+(++) b+++ DI+ D(+) G e+>++ h r--> y+
----- END GEEK CODE BLOCK-----
geekcode.comEDIT: Forgot to initialize i
[edited by - Neophyte on June 13, 2002 8:16:43 AM]