473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

references

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
Jul 23 '05 #1
8 2249
john townsley wrote:
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


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
Jul 23 '05 #2
"john townsley" <jo**********@optusnet.com.au> wrote in message
news:42**********************@news.optusnet.com.au ...
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


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
Jul 23 '05 #3
john townsley wrote:
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.


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.
Jul 23 '05 #4
David White wrote:
john townsley wrote:
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.
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.

cat main.cc #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;
}
g++ -Wall -ansi -pedantic -o main main.cc
./main &i is null
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'
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;
}
(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).


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).
Jul 23 '05 #5
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message
news:cu**********@nntp1.jpl.nasa.gov...
David White wrote:
However, a pointer can be null, but a reference can't.
> cat main.cc

#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;
}


You still do not have a null reference, and in the above case you cannot
have: f(0);
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'


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;
}
(if the pointer is not to const).
And your point is?
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).


In practice, this isn't a very useful observation.


Well, it's an observation. It's up to the individual to decide if it's a
useful one.
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).


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
Jul 23 '05 #6

"David White" <no@email.provided> wrote in message
news:2V*****************@nasal.pacific.net.au...
"john townsley" <jo**********@optusnet.com.au> wrote in message
news:42**********************@news.optusnet.com.au ...
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


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


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!
Jul 23 '05 #7
"john townsley" <jo**********@optusnet.com.au> wrote in message
news:42**********************@news.optusnet.com.au ...
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!


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

Jul 23 '05 #8
E. Robert Tisdale wrote:
However, a pointer can be null, but a reference can't.
> cat main.cc

#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);


Undefined behavior. You are attempting to dereference a null pointer.
return 0;
}
> g++ -Wall -ansi -pedantic -o main main.cc
> ./main

&i is null


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.

Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Tom | last post by:
The motivation for references seems clear: stop people from using nasty pointers when all they really want is a reference to an object. But C++ references are so inadequate that I'm still using...
22
by: xmp333 | last post by:
Hi All, I am trying to hide my JavaScript source. The method I chose was to keep all the important source in a password protected folder, and then use a SRC="folder/script.js" to include it...
33
by: JKop | last post by:
I understand variables/objects and pointer variables perfectly: int X = 5; int* pX = &X; *pX = 4; int** ppX = &pX:
2
by: S. van Beek | last post by:
Dear reader, For removing a reference in the VBA reference form I receive from Doug Steele the following code: ........... References.Remove refCurr
11
by: codebloatation | last post by:
I know how to use references but i DO not get WHY they exist other than to add to the language. Are they actually needed for anything?
14
by: el_sid | last post by:
Our developers have experienced a problem with updating Web References in Visual Studio.NET 2003. Normally, when a web service class (.asmx) is created, updating the Web Reference will...
30
by: jeremygetsmail | last post by:
I've got an adp (Metrix.adp) with a reference to another adp (InteractSQL.adp). InteractSQL sits on a server, and is refered to by all of the clients (Metrix), which sit on the client machines...
3
by: DonJefe | last post by:
Does anyone have experience using project->project references in large solutions? What are the plus/minuses that you have found? Currently, we are using the binary assembly references for our...
9
by: igor.kulkin | last post by:
References is a relatively basic feature of C++ language. It might be a good thing to think of references as aliases to the variables. However it's good to think of references this way when you...
3
by: CenturionX | last post by:
Hello everybody: I'd like to know what references in my vba code are used or not. I work in a code made by another person previously, i founded to many references and i believe that someones...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.