float foo; //Sorry, couldn''t think of a better name...
///////
// 1 //
///////
float square(float blah)
{
blah *= blah;
return blah;
}
foo = square(foo);
///////
// 2 //
///////
float square(float blah)
{
blah *= blah;
}
square(&foo);
///////
// 3 //
///////
float square(float &blah)
{
blah *= blah;
}
square(foo);
As far as I know, 2 and 3 would be exactly the same, storing the result in foo... but in 3 blah is taken as a reference without having to pass it as one in the function... But which of the 3 is the fastest?
I was just wondering... besides, I think square(&foo) makes me look cool too
Nick - Head Designer, Llamasoft.net
--
Visit our website...
Llamasoft.net
Games, goodies and ingenuity
References & returns in functions...
Hey all,
I was just thinking earlier, during an astoundingly boring history lesson () about function stuff... I haven''t really ever used references before, but I had alot of thinking time, so...
Basically, I was wondering if all three of these functions (and implementations demonstrated) have the same effect, and if so, which is the fastest? (Ignore the fact i''m only squaring it... I wanted a simple and crude example - no, i''d never use this as a real function )
Nick - Head Designer, Llamasoft.net--Visit our website...Llamasoft.netGames, goodies and ingenuity
A reference is a hidden pointer, so using them on base types takes longer that not using them
references are good for struct''s, so you don''t have to copy the whole thing.
so in this case #1 is the faste
I don''t think #2 will compile, &foo is a float* not a float
it needs to be
and #3 works just like #2, pointer & all
references are good for struct''s, so you don''t have to copy the whole thing.
so in this case #1 is the faste
I don''t think #2 will compile, &foo is a float* not a float
it needs to be
float square(float* blah){ *blah *= *blah;}square(foo);
and #3 works just like #2, pointer & all
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Well as Magmai Kai Holmor said all exemples work fine. You can think reference as hidden pointer or alias to variable,struct.
Don´t mess with this.
It kills you IF you return address to pointer which is Dead.
Beware also in reference it can´t be NULL as you might know.
Don´t mess with this.
//some function float * pSpeed; //Some code return pSpeed}
It kills you IF you return address to pointer which is Dead.
Beware also in reference it can´t be NULL as you might know.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement