D
Desmond Liu
I've read articles like Scott Meyer's EC++ (Item 22) that advocate the use
of references when passing parameters. I understand the reasoning behind
using references--you avoid the cost of creating a local object when you
pass an object by reference.
But why use a reference? Is there any inherent advantage of using a
reference over using a pointer? My workplace discourages the use of
pointers when it comes to parameter passing. They always prefer references.
I would argue one reason why we might consider using a pointer is to help
communicate intent. For example, suppose I do the following in C:
#include "foo.h"
int main(int argc, char *argv[])
{
int x=5;
foo(x);
printf("%d\n",x);
return 0;
}
I can guarantee that the program will print '5' because C only supports
pass-by-value. If we're using C++, I can't make that guarantee because
we're allowed to pass-by-reference.
But if I use pointers in parameter-passing, I can use the address-of
operator to communicate intent to other programmers that the parameter
is meant to be modified. For example,
#include "A.h"
#include "X.h"
int main(int argc, char *argv[])
{
A a;
X x;
x.foo(&a); // the intent of this member function is to modify 'a'
// A non-const member function of 'a' will be called.
x.bar(a); // this member function will NOT modify 'a'. Only const
// member functions of 'a' will be called.
std::cout << a << std::endl;
return 0;
}
If I'm having problems with object 'a', I see that member function bar()
will not change object 'a' (by convention), and that my problem is likely
in member function foo(). This is especially useful if I'm sifting through
a large amount of code to track down a problem. By using this convention, I
can save time not having to look at every member function in the class
declaration to see which ones might be the cause of my problem.
Of course, all programmers have to adhere to the convention in order for it
to be useful. So to make a long story short, is it better to do
this:
void x::foo(A* a); // foo() will modify 'a'
void x::bar(const A& a); // bar() will not modify 'a'
Or is it better to do what my workplace requires?
void x::foo(A& a); // foo() will modify 'a'
void x::bar(const A& a); // bar() will not modify 'a'
Am I wrong in suggesting the use of pointers for parameter-passing? Is
there a reason why a reference should always be preferred over a pointer?
My thinking is that this convention would make the C++ code more readable.
of references when passing parameters. I understand the reasoning behind
using references--you avoid the cost of creating a local object when you
pass an object by reference.
But why use a reference? Is there any inherent advantage of using a
reference over using a pointer? My workplace discourages the use of
pointers when it comes to parameter passing. They always prefer references.
I would argue one reason why we might consider using a pointer is to help
communicate intent. For example, suppose I do the following in C:
#include "foo.h"
int main(int argc, char *argv[])
{
int x=5;
foo(x);
printf("%d\n",x);
return 0;
}
I can guarantee that the program will print '5' because C only supports
pass-by-value. If we're using C++, I can't make that guarantee because
we're allowed to pass-by-reference.
But if I use pointers in parameter-passing, I can use the address-of
operator to communicate intent to other programmers that the parameter
is meant to be modified. For example,
#include "A.h"
#include "X.h"
int main(int argc, char *argv[])
{
A a;
X x;
x.foo(&a); // the intent of this member function is to modify 'a'
// A non-const member function of 'a' will be called.
x.bar(a); // this member function will NOT modify 'a'. Only const
// member functions of 'a' will be called.
std::cout << a << std::endl;
return 0;
}
If I'm having problems with object 'a', I see that member function bar()
will not change object 'a' (by convention), and that my problem is likely
in member function foo(). This is especially useful if I'm sifting through
a large amount of code to track down a problem. By using this convention, I
can save time not having to look at every member function in the class
declaration to see which ones might be the cause of my problem.
Of course, all programmers have to adhere to the convention in order for it
to be useful. So to make a long story short, is it better to do
this:
void x::foo(A* a); // foo() will modify 'a'
void x::bar(const A& a); // bar() will not modify 'a'
Or is it better to do what my workplace requires?
void x::foo(A& a); // foo() will modify 'a'
void x::bar(const A& a); // bar() will not modify 'a'
Am I wrong in suggesting the use of pointers for parameter-passing? Is
there a reason why a reference should always be preferred over a pointer?
My thinking is that this convention would make the C++ code more readable.