* LuB:
I wanted to use the most efficient argument passing method.
Premature optimization is the root of all evil. And anyway, what you ask for
here is a bit advanced. If you absolutely want to do it, refer to 'Modern C++
Design' for a discussion of how to partially automate the selection of how to
do argument passing, based on size and in/out/in-out requirements.
I was
always taught that its best to pass (const SomeObject& obj) if possible
No.
... but in this case, I can't pass a const param since I will be
modifying the parameter in the function.
Eg: 1
void foo(SomeObject& obj)
{
obj.doSomething();
}
or Eg: 2
void foo(SomeObject* obj)
{
obj->doSomething();
}
Aside from the NULL PTR safety of the reference version, is there a
performance advantage in the way the function and parameters are
placed/copied onto the stack for either version?
No, but I can imagine the reference version to be easier to optimize (because
no analysis is needed to check what the reference refers to in each
statement), so it might be that it's better optimized with some compilers.
However, the NULL pointer safety is important, and there are other
non-efficiency related issues.
One particularly important such issue is that the reference cannot be
re-seated within the function. Another is that arithmetic cannot be performed
on the reference, only on the object it refers to (think of e.g. a std::string
argument). A third is that the reference signature clearly indicates a single
object, whereas the pointer signature matches object or array or null.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?