are there any differences when using pointers or passing by reference when
using
1) basic variables
2)complex variable types like arrays,structur es
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 8 2262
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,structur es
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
"john townsley" <jo**********@o ptusnet.com.au> wrote in message
news:42******** **************@ news.optusnet.c om.au... are there any differences when using pointers or passing by reference when using
1) basic variables 2)complex variable types like arrays,structur es
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
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.
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,structur es.
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).
"E. Robert Tisdale" <E.************ **@jpl.nasa.gov > wrote in message
news:cu******** **@nntp1.jpl.na sa.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
"David White" <no@email.provi ded> wrote in message
news:2V******** *********@nasal .pacific.net.au ... "john townsley" <jo**********@o ptusnet.com.au> wrote in message news:42******** **************@ news.optusnet.c om.au... are there any differences when using pointers or passing by reference when using
1) basic variables 2)complex variable types like arrays,structur es
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!
"john townsley" <jo**********@o ptusnet.com.au> wrote in message
news:42******** **************@ news.optusnet.c om.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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 pointers for my
references to objects. There are 3 reasons why I find them inadequate:
1 - Can't be null.
2 - Can't be reseated.
Now I'm sure...
|
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 in my code. This way, the
script will run, but the user will be unable to view the included
code. Or so I think :).
I have tried this method,...
|
by: JKop |
last post by:
I understand variables/objects and pointer variables perfectly:
int X = 5;
int* pX = &X;
*pX = 4;
int** ppX = &pX:
|
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
|
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?
| |
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 utilise the disco file to update the
Corresponding proxy file and reflect the changes made to the web service.
However, the results of doing this with...
|
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 (There's
also a SQL Server that sits on the server, but that's besides the point
here.). Both adp files have references to ADOX. I've got to check...
|
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 code,
but this becomes problematic when you want to build from multiple
branches/working directories.
Any thoughts on this would be appreciated.
...
|
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 deal with
references which are local variables.
But references can also be function arguments (in fact they are more
useful this way) in which case...
|
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 are not used.
I'd like to optimize the application ereasing the useless references.
Thanks a lot.
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |