| re: inline functions and return by reference.
Sreenivas posted:
[color=blue]
> Hi,
> We cannot return a reference to an automatic variable from a function,
> as per the ANSI C++ standard the behaviour is undefined. Does this hold
> for inline functions too? or can I return a reference to the automatic
> variable from the inline function. I meant automatic variable by stack
> variable strictly local to that function.
> Thanks,
> Sreenivas.[/color]
The following two functions are equivalent in that they give the caller
access to an object which has already been destroyed:
int& Monkey()
{
int k = 4;
return k;
}
int& Ape()
{
int k = 4;
return &k;
}
As regards inline functions, they work exactly as do everyday functions.
Their static variables work exactly the same too. There's only two
differences between an inline function and a normal function:
1) It has "inline" written before it.
2) You're not violating the One Definition Rule if you define it more than
once, eg. the following is *il*legal:
int Blah() { return 5; }
int Blah() { return 5; }
While the following is perfectly legal:
inline int Blah() { return 5; }
inline int Blah() { return 5; }
(Or maybe I'm mistaken there? Perhaps you can define inline functions as
many times as you want... except it must be once per translation unit?)
Anyway, the result is that you can stick an inline function in a header
file!
-JKop |