Connecting Tech Pros Worldwide Forums | Help | Site Map

references

john townsley
Guest
 
Posts: n/a
#1: Jul 23 '05
are there any differences when using pointers or passing by reference when
using

1) basic variables
2)complex variable types like arrays,structures

it seems easier to simply pass by reference and simply forget about pointers
but thats seems to easy, there must still be a need for pointers



Karsten Baumgarten
Guest
 
Posts: n/a
#2: Jul 23 '05

re: references


john townsley wrote:[color=blue]
> are there any differences when using pointers or passing by reference when
> using
>
> 1) basic variables
> 2)complex variable types like arrays,structures
>
> it seems easier to simply pass by reference and simply forget about pointers
> but thats seems to easy, there must still be a need for pointers
>
>[/color]

The difference is that a reference to an object basically is the object
itself, a pointer to an object is a pointer, not the object.

--
Regards,

Karsten
David White
Guest
 
Posts: n/a
#3: Jul 23 '05

re: references


"john townsley" <johntownsley@optusnet.com.au> wrote in message
news:42081a24$0$1022$afc38c87@news.optusnet.com.au ...[color=blue]
> are there any differences when using pointers or passing by reference when
> using
>
> 1) basic variables
> 2)complex variable types like arrays,structures
>
> it seems easier to simply pass by reference and simply forget about[/color]
pointers[color=blue]
> but thats seems to easy, there must still be a need for pointers[/color]

For the most obvious implementations of pointers and references, performance
is unlikely to be affected. However, a pointer can be null, but a reference
can't. Sometimes you might want to use a pointer so you can pass a null
pointer. Pointers can also be re-seated, but references can't. However,
pointers passed to functions usually aren't re-seated. Pointers can result
in uglier, more verbose code than references because a pointer needs to be
dereferenced, sometimes many times. You might also want to consider the call
to the function:
f(&anObject); // pointer
f(anObject); // reference
In the pointer case it's obvious from the call that an address is being
passed, and therefore that the function might change 'anObject' (if the
pointer is not to const). In the reference case you can't tell from the call
whether it's pass by value ('anObject' has to copied and cannot be changed)
or pass by reference (it is not copied and can be changed).

DW


E. Robert Tisdale
Guest
 
Posts: n/a
#4: Jul 23 '05

re: references


john townsley wrote:
[color=blue]
> Are there any differences when using pointers
> or passing by reference when using
>
> 1) basic variables or
> 2) complex variable types like arrays, structures
>
> It seems easier to simply pass by reference and simply forget about pointers
> but that seems too easy. There must still be a need for pointers.[/color]

Pass by reference and passing pointers are implemented the same way --
the compiler emits code to pass the address of the object.

There is never any reason to prefer passing a pointer
instead of passing by reference.

When you pass a complicated object:

struct X {
private:
// complicated representation
int I;
public:
// complicated functions
get(void) const;
};

by reference:

void f(const X& x) {
int i = x.get();
}

you can reference member functions with operator.
But, when you pass a pointer to a complicated object

void g(const X* p) {
const
X& x = *p;
int j = x.get();
int k = (*p).get();
int i = p->get();
}

you must use operator-> or
convert const pointer p to a const reference (*p or x)
so that you can use operator.
E. Robert Tisdale
Guest
 
Posts: n/a
#5: Jul 23 '05

re: references


David White wrote:
[color=blue]
> john townsley wrote:
>[color=green]
>>Are there any differences when using pointers or passing by reference
>>when using
>>
>> 1) basic variables or
>> 2) complex variable types like arrays,structures.
>>
>>It seems easier to simply pass by reference
>>and simply forget about pointers but that seems to easy.
>>There must still be a need for pointers.[/color]
>
> For the most obvious implementations of pointers and references,
> performance is unlikely to be affected.
> However, a pointer can be null, but a reference can't.[/color]
[color=blue]
> cat main.cc[/color]
#include <iostream>

void f(const int& i) {
if (0 == &i)
std::cerr << "&i is null" << std::endl;
else
std::cerr << "&i is null" << std::endl;
}

int
main(int argc, char* argv[]) {
int* p = 0;
f(*p);
return 0;
}
[color=blue]
> g++ -Wall -ansi -pedantic -o main main.cc
> ./main[/color]
&i is null
[color=blue]
> Sometimes you might want to use a pointer so you can pass a null pointer.
> Pointers can also be re-seated, but references can't.
> However, pointers passed to functions usually aren't re-seated.
> Pointers can result in uglier, more verbose code than references
> because a pointer needs to be dereferenced, sometimes many times.
> You might also want to consider the call to the function:[/color]
[color=blue]
> f(&anObject); // pointer
> f(anObject); // reference[/color]
[color=blue]
> In the pointer case it's obvious from the call
> that an address is being passed and, therefore, that
> the function might change 'anObject'[/color]

class X {
// . . .
};

void f(const X&);
void g(const X*);

int main(int argc, char* argv[]) {
const
X x; // large object
// . . .
f(x);
g(&x);
return 0;
}
[color=blue]
> (if the pointer is not to const).
> In the reference case, you can't tell from the call
> whether it's pass by value ('anObject' has to copied and cannot be changed)
> or pass by reference (it is not copied and can be changed).[/color]

In practice, this isn't a very useful observation.
Small objects, such as the built-in types,
are usually always passed by value.
Large opbjects, such as structures and arrays
are usually always passed by reference
(or by reference through a pointer).
David White
Guest
 
Posts: n/a
#6: Jul 23 '05

re: references


"E. Robert Tisdale" <E.Robert.Tisdale@jpl.nasa.gov> wrote in message
news:cu9cth$j40$1@nntp1.jpl.nasa.gov...[color=blue]
> David White wrote:
>[color=green]
> > However, a pointer can be null, but a reference can't.[/color]
>[color=green]
> > cat main.cc[/color]
> #include <iostream>
>
> void f(const int& i) {
> if (0 == &i)
> std::cerr << "&i is null" << std::endl;
> else
> std::cerr << "&i is null" << std::endl;
> }
>
> int
> main(int argc, char* argv[]) {
> int* p = 0;
> f(*p);
> return 0;
> }[/color]

You still do not have a null reference, and in the above case you cannot
have: f(0);
[color=blue][color=green]
> > You might also want to consider the call to the function:[/color]
>[color=green]
> > f(&anObject); // pointer
> > f(anObject); // reference[/color]
>[color=green]
> > In the pointer case it's obvious from the call
> > that an address is being passed and, therefore, that
> > the function might change 'anObject'[/color]
>
> class X {
> // . . .
> };
>
> void f(const X&);
> void g(const X*);
>
> int main(int argc, char* argv[]) {
> const
> X x; // large object
> // . . .
> f(x);
> g(&x);
> return 0;
> }
>[color=green]
> > (if the pointer is not to const).[/color][/color]

And your point is?
[color=blue][color=green]
> > In the reference case, you can't tell from the call
> > whether it's pass by value ('anObject' has to copied and cannot be[/color][/color]
changed)[color=blue][color=green]
> > or pass by reference (it is not copied and can be changed).[/color]
>
> In practice, this isn't a very useful observation.[/color]

Well, it's an observation. It's up to the individual to decide if it's a
useful one.
[color=blue]
> Small objects, such as the built-in types,
> are usually always passed by value.
> Large opbjects, such as structures and arrays
> are usually always passed by reference
> (or by reference through a pointer).[/color]

The fact remains that in the reference case you can't tell how the argument
is passed. Some might consider that a disadvantage. I did for a while, but
not any more. The OP might or might not, but it is something to consider.

DW


john townsley
Guest
 
Posts: n/a
#7: Jul 23 '05

re: references



"David White" <no@email.provided> wrote in message
news:2VVNd.7349$i6.67616@nasal.pacific.net.au...[color=blue]
> "john townsley" <johntownsley@optusnet.com.au> wrote in message
> news:42081a24$0$1022$afc38c87@news.optusnet.com.au ...[color=green]
>> are there any differences when using pointers or passing by reference
>> when
>> using
>>
>> 1) basic variables
>> 2)complex variable types like arrays,structures
>>
>> it seems easier to simply pass by reference and simply forget about[/color]
> pointers[color=green]
>> but thats seems to easy, there must still be a need for pointers[/color]
>
> For the most obvious implementations of pointers and references,
> performance
> is unlikely to be affected. However, a pointer can be null, but a
> reference
> can't. Sometimes you might want to use a pointer so you can pass a null
> pointer. Pointers can also be re-seated, but references can't. However,
> pointers passed to functions usually aren't re-seated. Pointers can result
> in uglier, more verbose code than references because a pointer needs to be
> dereferenced, sometimes many times. You might also want to consider the
> call
> to the function:
> f(&anObject); // pointer
> f(anObject); // reference
> In the pointer case it's obvious from the call that an address is being
> passed, and therefore that the function might change 'anObject' (if the
> pointer is not to const). In the reference case you can't tell from the
> call
> whether it's pass by value ('anObject' has to copied and cannot be
> changed)
> or pass by reference (it is not copied and can be changed).
>
> DW
>
>[/color]

why then would you use pointers if you can pass by reference , I still see a
lot of people using pointers so there must be a need.....so whats the
point!


David White
Guest
 
Posts: n/a
#8: Jul 23 '05

re: references


"john townsley" <johntownsley@optusnet.com.au> wrote in message
news:42085e62$0$2672$afc38c87@news.optusnet.com.au ...[color=blue]
> why then would you use pointers if you can pass by reference , I still see[/color]
a[color=blue]
> lot of people using pointers so there must be a need.....so whats the
> point![/color]

I gave arguments for and against using pointers. I can't think of any more.
If you believe after reading the responses that references are always
better, please explain why. As for why other people use pointers at times,
it could be for one of the reasons I gave, or for some other reason, or
because that's what they are used to from C.

DW



Rolf Magnus
Guest
 
Posts: n/a
#9: Jul 23 '05

re: references


E. Robert Tisdale wrote:
[color=blue][color=green]
>> However, a pointer can be null, but a reference can't.[/color]
>[color=green]
> > cat main.cc[/color]
> #include <iostream>
>
> void f(const int& i) {
> if (0 == &i)
> std::cerr << "&i is null" << std::endl;
> else
> std::cerr << "&i is null" << std::endl;
> }
>
> int
> main(int argc, char* argv[]) {
> int* p = 0;
> f(*p);[/color]

Undefined behavior. You are attempting to dereference a null pointer.
[color=blue]
> return 0;
> }
>[color=green]
> > g++ -Wall -ansi -pedantic -o main main.cc
> > ./main[/color]
> &i is null[/color]

Even if you ignore the fact that it's undefined behavior (it probably works
on many implementations), this is very inelegant and irritating to anyone
reading the code. I strongly advice against doing that.

Closed Thread