473,756 Members | 3,686 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 3536

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
2591
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
16241
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
2539
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
10704
by: Ramasubramanian XR (AS/EAB) | last post by:
What is diff b/w malloc,calloc,realloc could any one explain
6
1721
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
1942
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
9456
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9275
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
10034
Oralloy
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9872
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
9843
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
9713
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
6534
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3805
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
2
3358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.