473,734 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what is the diff between pointer and reference

man
can any one please tell me what is the diff between pointer and
reference.....a nd which one is better to use ....and why???????

Nov 22 '05 #1
18 3531

man wrote:
can any one please tell me what is the diff between pointer and
reference.....a nd which one is better to use ....and why???????


http://www.parashift.com/c++-faq-lite/references.html

Nov 22 '05 #2
Hi,

a reference is like an alias. A different name for the same thing (i.e.
object or function). A Pointer points to the physical address of a
variable/object.

bye,

Oliver

Nov 22 '05 #3
Adding to Oliver post, refferences should always be initialised,
pointer need not be.

they again have many adavantages and disadvantages.. .more given in book

Oliver Block wrote:
Hi,

a reference is like an alias. A different name for the same thing (i.e.
object or function). A Pointer points to the physical address of a
variable/object.

bye,

Oliver


Nov 22 '05 #4
ryadav wrote:
Adding to Oliver post, refferences should always be initialised,
pointer need not be.
This is because a reference is an alias( as already mentioned ). So
this is not a different point that you are putting down.

they again have many adavantages and disadvantages.. .more given in book


Which book ? You are not initializing a _reference_ here :-)

Nov 22 '05 #5
On 2005-11-16, man <ma***********@ gmail.com> wrote:
can any one please tell me what is the diff between pointer and
reference.....a nd which one is better to use ....and why???????


When choosing a type to refer to an object:

Use a reference when you have to, like in copy constructors.
Use a reference when the referenced object will always exist.
Use a reference when the reference will not be changed to refer
to a different object.

Otherwise, use a pointer.

Apologies to Steve Meyers. See _More Effective C++_ for details.

--
Neil Cerutti
Nov 22 '05 #6
In article <11************ **********@f14g 2000cwb.googleg roups.com>,
Sandeep <sa************ @gmail.com> wrote:
ryadav wrote:
Adding to Oliver post, refferences should always be initialised,
pointer need not be.


This is because a reference is an alias( as already mentioned ). So
this is not a different point that you are putting down.


Actually, it is a big difference. The concept of a reference
being an alias did not have to mean that it had to be initialized,
however, it was determined that for C++ that having it that way
was a good idea. Also, there are other ways in C++ to establish
aliases, and they do not have such a syntactic demand, perhaps
unfortunately.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Nov 22 '05 #7
Greg Comeau wrote:
In article <11************ **********@f14g 2000cwb.googleg roups.com>,
Sandeep <sa************ @gmail.com> wrote:
ryadav wrote:
Adding to Oliver post, refferences should always be initialised,
pointer need not be.


This is because a reference is an alias( as already mentioned ). So
this is not a different point that you are putting down.


Actually, it is a big difference. The concept of a reference
being an alias did not have to mean that it had to be initialized,
however, it was determined that for C++ that having it that way
was a good idea. Also, there are other ways in C++ to establish
aliases, and they do not have such a syntactic demand, perhaps
unfortunately.


If you see carefully I am reponding to the statement saying that apart
from being an alias, an _additional_ difference is that it needs to be
initialized. And while doing this I am giving the reason why it needs
to be initialized.
I agree to what you say about there being other ways to establish (
initialize :-) ) a reference.

When you say "The concept of a reference being an alias did not have
to mean that it had to be initialized", I guess we are trying to say
the same things. Apologies if I was not clear.

Nov 22 '05 #8
Al
Why do you often use constant references? What is the point of doing
that?
I heard of being faster because of not tranfering and copying the
object when called as argument... more details?
ex: T(const T& r): a(r.a), b(r.b) {} instead of T(T r): a(r.a), b(r.b)
{}

or many other cases
why is it so important to do it?

Nov 22 '05 #9

Al wrote:
Why do you often use constant references? What is the point of doing
that?
I heard of being faster because of not tranfering and copying the
object when called as argument... more details?
ex: T(const T& r): a(r.a), b(r.b) {} instead of T(T r): a(r.a), b(r.b)
{}

or many other cases
why is it so important to do it?


Well you've got it really. The difference between your two examples
above is that the first does not need to make a copy whereas the second
does. If copying is non-trivial and you do not *need* a copy then why
waste time making one? Using a reference does not make the code any
harder to read - the idiom is extremely widespread and recognisable.

So that's the advantage of a reference. Why use a const reference?
Firstly, the compiler will prevent you from introducing bugs where you
try to modify the object[*] and secondly it allows you to pass const
objects and temporaries.

Gavin Deane
[*] If you need to be able to modify the object then you can't use a
const reference. If you need to modify a copy of the object but leave
the original untouched then you can't use a reference at all - you need
a copy. But if you don't need to modify the object at all, using a
const reference gets the compiler helping you achieve that. The
compiler is, after all, your friend.

Nov 22 '05 #10

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

Similar topics

16
2589
by: Jason | last post by:
Hey, I'm an experience programmer but new to Python. I'm doing a simple implementation of a field morphing techinique due to Beier and Neely (1992) and I have the simple case working in Python 2.3 - but it's REALLY slow. Basically, you specify two directed line segments in the coordinate system of a raster image and use the difference between those two lines to transform the image.
2
16240
by: CoolPint | last post by:
Can anyone clearly explain the difference between constant reference to pointers and reference to constant pointers? What is const int * & ? Is it a constant reference to a pointer to an integer? Or Is it a reference to a pointer to a constant integer? What is being constant in this case? The pointer or the integer being pointed? How about int * const & ? Is this a reference to a constant pointer to an integer? Or
14
2536
by: bo | last post by:
And why and where one should use one vs. the other? Verbally, it seems like semantics to me--but obviously there is some actual difference that makes references different and or preferable over pointers in some cases... TIA
6
10702
by: Ramasubramanian XR (AS/EAB) | last post by:
What is diff b/w malloc,calloc,realloc could any one explain
6
1718
by: wenmang | last post by:
Here is the code which causes core dump when is built differently, 1 for regular dev built, the other for packaging build. class my_class{ public: my_class(){}; ~my_class(){}; private: string myData1;
17
1941
by: Rob Hoelz | last post by:
I've been compiling a list of the differences between C and C++, and I'd like to see how thorough I've been. Could any of you go over this list and see if I've missed anything? Here's the list: Mid-scope declarations Default function parameters new/delete operators Better implementation of const
4
6694
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader = "Content-Type: text/html\r\n"; Why is the second const needed and what does it do? Thanks
11
3360
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible ways of passing an array. In the following code, fun1(int a1) - same as fun1(int* a1) - where both are of the type passed by reference. Inside this function, another pointer a1 is created whose address &a1 is different from that of the passed...
0
8783
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9317
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9248
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9187
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8192
agi2029
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6743
isladogs
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4555
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2184
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.