PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > pointer vs reference
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
pointer vs reference

Réponse
 
LinkBack Outils de la discussion
Vieux 05/04/2008, 01h13   #1
Eric Kaplan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut pointer vs reference

what are the difference between the following 4 variables?

const int * start
int* const start
int const& start
Request(int const& start);
  Réponse avec citation
Vieux 05/04/2008, 01h20   #2
Jim Langston
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: pointer vs reference

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
tazmaster@rocketmail.com


  Réponse avec citation
Vieux 05/04/2008, 01h25   #3
Sam
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: pointer vs reference

Eric Kaplan writes:

> what are the difference between the following 4 variables?
>
> const int * start
> int* const start
> int const& start
> Request(int const& start);


Can you try doing your homework assignment by yourself, first?



-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQBH9sbvx9p3GYHlUOIRAjRGAJ0We529jMHIy44y3SIq9G GwX/4YFgCdGxkV
MQOtv7vxRuSrM00NEq8aTYk=
=FQbT
-----END PGP SIGNATURE-----

  Réponse avec citation
Vieux 05/04/2008, 01h26   #4
Eric Kaplan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: pointer vs reference

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);
  Réponse avec citation
Vieux 05/04/2008, 01h30   #5
Eric Kaplan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: pointer vs reference

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)
  Réponse avec citation
Vieux 05/04/2008, 01h33   #6
Jim Langston
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: pointer vs reference

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
tazmaster@rocketmail.com


  Réponse avec citation
Vieux 05/04/2008, 01h34   #7
Eric Kaplan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: pointer vs reference

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).

  Réponse avec citation
Vieux 05/04/2008, 01h54   #8
Jim Langston
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: pointer vs reference

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
tazmaster@rocketmail.com


  Réponse avec citation
Vieux 05/04/2008, 01h57   #9
Jim Langston
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: pointer vs reference

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
tazmaster@rocketmail.com


  Réponse avec citation
Vieux 05/04/2008, 02h46   #10
Eric Kaplan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: pointer vs reference

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";
}
  Réponse avec citation
Vieux 05/04/2008, 10h14   #11
Erik Wikström
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: pointer vs reference

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
  Réponse avec citation
Vieux 05/04/2008, 13h25   #12
Pete Becker
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: pointer vs reference

On 2008-04-04 20:20:30 -0400, "Jim Langston" <tazmaster@rocketmail.com> said:

>
>> const int * start

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

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


The value of the int can change, but you can't change it through 'start'.

int i = 0;
const int *start = &i;
std::cout << *start << '\n'; // 0
++i; // OK
std::cout << *start << '\n'; // 1
++*start; // illegal

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

  Réponse avec citation
Vieux 05/04/2008, 19h54   #13
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: pointer vs reference

On 5 avr, 02:33, "Jim Langston" <tazmas...@rocketmail.com> wrote:
> 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.


[...]

Good posting, but you missed the two most obvious differences
from the user's point of view (and thus, to take into
consideration when designing the interface):

-- A reference cannot be null; it must designate an actual
object. If you want an optional value, you'll have to use a
pointer.

-- A pointer cannot be initialized to point to a temporary. If
you want your client to be able to pass the results of an
arbitrary expression, then you must use a reference to
const.

Because of these considerations, from a user's point of view, a
reference to a const is actually more like pass by value than a
pointer, i.e. given:

void func1( std::string ) ;
void func2( std::string const& ) ;
void func3( std::string const* ) ;

, there is almost no difference between the first two for the
user; it is the third that is different.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
  Réponse avec citation
Vieux 07/04/2008, 19h59   #14
Matthias Buelow
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: pointer vs reference

James Kanze wrote:

> -- A reference cannot be null; it must designate an actual
> object. If you want an optional value, you'll have to use a
> pointer.


What about: int &r = *(int *)0;
  Réponse avec citation
Vieux 07/04/2008, 20h38   #15
Bo Persson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: pointer vs reference

Matthias Buelow wrote:
> James Kanze wrote:
>
>> -- A reference cannot be null; it must designate an actual
>> object. If you want an optional value, you'll have to use a
>> pointer.

>
> What about: int &r = *(int *)0;


No, that is invalid. As soon as you dereference a null pointer, your
code has undefined behavior. This happens before you get a reference
to null.


Bo Persson


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h12.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,17394 seconds with 23 queries