Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > pointer vs reference

Reply
Thread Tools

pointer vs reference

 
 
Eric Kaplan
Guest
Posts: n/a
 
      04-05-2008
what are the difference between the following 4 variables?

const int * start
int* const start
int const& start
Request(int const& start);
 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      04-05-2008
Eric Kaplan wrote:
> what are the difference between the following 4 variables?


Read right to left.

> const int * start


start is a pointer to an int that is constant (the int doesn't change).

> int* const start


start is a constant pointer to an int (the pointer doesn't change)

> int const& start


start is a reference to a constant int (the int doesn't change).

> Request(int const& start);


start is a reference to a constant int (the int doesn't change).

--
Jim Langston
http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
 
 
 
Eric Kaplan
Guest
Posts: n/a
 
      04-05-2008
more specific - for passing parameter, what's different between & OR *

pointer vs reference ??

Request(string const& start, string const& end);
Request(string const* start, string const* end);
 
Reply With Quote
 
Eric Kaplan
Guest
Posts: n/a
 
      04-05-2008
I suppose the following is exact same thing right?

int * const start
int const * start

both means the pointer is constant - (pointer always point to same
address)
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      04-05-2008
Eric Kaplan wrote:
> more specific - for passing parameter, what's different between & OR *
>
> pointer vs reference ??
>
> Request(string const& start, string const& end);
> Request(string const* start, string const* end);


A reference is an alias. It is similar to a pointer but with some changes.
The most obvious is using a reference you don't have to dereference the
variable to get the value.

void Foo( int* Bax )
{
// To get the value of the integer we have to derefernce the pointer.
std::cout << *Bax << "\n";
}

void Bar( int& Bax )
{
// The get the value of the integer we just use the variable
std::cout << Bax << "\n";
}

There are other differences, such that a refernce must be initialized. It
can not have an unitialized value. Nor can a reference be reseated. Once a
reference is created it points to something, and will continue to point to
that thing until it is destroyed/goes out of scope.

int A = 10;
int B = 20;
int* Foo = &A;
int& Bar = A;

Foo = &B; // legal. Foo now points to B.
Bar = B; // legal, but te value of A changed, not where Bar points.

For any futher information I would suggest you read a book or search the
web.




--
Jim Langston
(E-Mail Removed)


 
Reply With Quote
 
Eric Kaplan
Guest
Posts: n/a
 
      04-05-2008
so start is a reference = memory address?

content of start is likely to be something like -
0x12349870h

??

>> int const& start

>
>start is a reference to a constant int (the int doesn't change).

 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      04-05-2008
Eric Kaplan wrote:
> so start is a reference = memory address?
>
> content of start is likely to be something like -
> 0x12349870h
>
> ??
>
>>> int const& start

>>
>> start is a reference to a constant int (the int doesn't change).


A reference doesn't actually exist. A reference is an alias. The compiler
is free to do that however it wishes. The compiler may actually use the
original variable, or the address, or some other method. You shouldn't
count on how the compiler does it as it may change from compiler to compiler
and even from compiler version to version.

--
Jim Langston
(E-Mail Removed)


 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      04-05-2008
Eric Kaplan wrote:
> I suppose the following is exact same thing right?
>
> int * const start
> int const * start
>
> both means the pointer is constant - (pointer always point to same
> address)


No. Those are not the same.
const int* start;
int const* start;
are exactly the same.

int* const start;
start is a constant *pointer* to an int. You can not reseat the poniter.
You can not change where the pointer is pointing to. In fact you better
initialize it since you can't change it. But you can change the integer
that the pointer points to.

int const * start;
start is a pointer to a constant *integer* You can not change the value of
what the pointer points to, but you are free to make the pointer point to
some other memory location.


--
Jim Langston
(E-Mail Removed)


 
Reply With Quote
 
Eric Kaplan
Guest
Posts: n/a
 
      04-05-2008
Is there are time where I should not use a pointer parameter or
reference parameter?

OR it's just a matter of my taste? both will do the same thing right??

basically both doesn't pass a copy of value.

================

void Foo( int* Bax )
{
// To get the value of the integer we have to derefernce the
pointer.
std::cout << *Bax << "\n";
}

void Bar( int& Bax )
{
// The get the value of the integer we just use the variable
std::cout << Bax << "\n";
}
 
Reply With Quote
 
Erik Wikström
Guest
Posts: n/a
 
      04-05-2008
On 2008-04-05 03:46, Eric Kaplan wrote:
> Is there are time where I should not use a pointer parameter or
> reference parameter?
>
> OR it's just a matter of my taste? both will do the same thing right??
>
> basically both doesn't pass a copy of value.


Right, so when you create a function you need to decide how to pass the
arguments, the first choice is whether to pass by value or by reference
(using either a reference or a pointer), and it seem like you know the
difference already. If you decide to pass by reference you have to
choose between references and pointers, my rule of thumb is to use
references whenever I can and pointers only when I must (there are some
things that can only be achieved by using pointers).

Notice also that for small types (like the builtin once and other small
classes) it might sometimes be faster to pass a const copy instead of a
reference. If you need speed use a profiler and test which is the
fastest in your case.

--
Erik Wikström
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Reference counting and API (const reference vs pointer oriented) mathieu C++ 8 08-31-2008 09:05 AM
reference and pointer-reference kasthurirangan.balaji@gmail.com C++ 4 12-24-2007 09:05 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM
Passing the value by reference is same as pointer by reference sam pal C++ 3 07-16-2003 09:14 PM



Advertisments