Question: Do you use to return objects from functions? Is there anything wrong with this practice? Example: pastebin.com/x1SB2bZ6
Asked by Rodrigo (189.26.130.x) on August 13 2011, 12:33pm
Reply on August 13 2011, 5:22pm (edited at August 13 2011, 7:51pm):I don't usually return structs, just out of habit, since at one point in my life it wasn't always possible.
As far as performance/code generation go goes, assuming the structure type is simple (and doesn't have copy constructors, destructors, etc), it appears that they should compile to very close to the same code. Note that the form of:
RECT r = someFunc(); // or: RECT r; someFunc2(&r);
printf("%d %d",r.left,r.top);
is often faster / smaller than:
printf("%d %d",someFunc().left,someFunc().top);
...due to the fact that the compiler might call the code in someFunc twice, unless it is a trivially inlineable function that does change any state... that style is relying more on the compiler.
Comment: