Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > References or pointers?

Reply
Thread Tools

References or pointers?

 
 
desktop
Guest
Posts: n/a
 
      09-05-2007
I have made this example:

#include<iostream>

class Beer {
public:
Beer() {
std::cout << "made a beer\n";
num = 1;
}

Beer(int n) : num(n) {}

int getBeer(){
return num;
}

void remove(){
num--;
}

private:
int num;
};

class Brew {
public:
/* Simple*/
void drinkBeer(Beer b) {
std::cout << "Drank " << b.getBeer() << " instances of beers!\n";
b.remove();
}

void checkBeer(Beer b) {
std::cout << b.getBeer() << " instances of beers left!\n";
}




/* With references. Notice that the '&' is placed AFTER the type like the
* pointer operator. */
void drinkBeerRef(Beer& b) {
std::cout << "Drank " << b.getBeer() << " reference beers!\n";
b.remove();
}

void checkBeerRef(Beer& b) {
std::cout << b.getBeer() << " reference beers left!\n";
}



/* With pointers */
void drinkBeer(Beer* b) {
std::cout << "Drank " << b->getBeer() << " pointer beers!\n";
b->remove();
}

void checkBeer(Beer* b) {
std::cout << b->getBeer() << " pointer beers left!\n";
}
};


int main(){

/* (0) Calling default constructor. */
Beer b1;

/* (1) Make a brewery where you can drink! checkBeer will not print
* the right value. */
Beer b2(10); // make 10 beers.
Brew brew;
brew.drinkBeer(b2);
brew.checkBeer(b2);

std::cout << std::endl;


/* (2) Using references */
Beer b3(10); // make 10 beers.
brew.drinkBeerRef(b3);
brew.checkBeerRef(b3);

std::cout << std::endl;

/* (3) Using pointers */

/* Single object containing 10 beers. */
Beer* b4 = new Beer(10);

/* or:
*
* Beer* b4 = new Beer[10];
*
* using default constructor. 10 objects containing 1 beer.
*
* */

brew.drinkBeer(b4);
brew.checkBeer(b4);

return 0;
}


How does passing an argument as a reference differ from passing it as a
pointer (besides from also giving the right result)?

When you use 'new' the data is allocated on the heap and when changed in
a function will remain changed after the functions returns. But this is
also the case with references...does 'Beer b3' allocate 'b3' on the heap?
 
Reply With Quote
 
 
 
 
Carlo Capelli
Guest
Posts: n/a
 
      09-05-2007

"desktop" <(E-Mail Removed)> ha scritto nel messaggio
news:fbljr2$48m$(E-Mail Removed)-c.dk...
>I have made this example:

....
> How does passing an argument as a reference differ from passing it as a
> pointer (besides from also giving the right result)?


You can think to references as 'syntax sugar' for const pointers (beware,
not pointer to consts).
Passing an argument by reference is a safer and more convenient way to pass
the pointer to the value.
The callee can modify the value but not the pointer, i.e. can't switch the
'pointer' to something else.

> When you use 'new' the data is allocated on the heap and when changed in a
> function will remain changed after the functions returns. But this is also
> the case with references...does 'Beer b3' allocate 'b3' on the heap?


b3 is on the stack, but the pertinent point is that the section marked /*
Simple*/ it's incorrect.
You pass 'by value' the beer, and that make sense only in the checkBeer
call.

Bye Carlo


 
Reply With Quote
 
 
 
 
terminator
Guest
Posts: n/a
 
      09-05-2007
On Sep 5, 9:54 am, desktop <(E-Mail Removed)> wrote:
> I have made this example:
>
> #include<iostream>
>
> class Beer {
> public:
> Beer() {
> std::cout << "made a beer\n";
> num = 1;
> }
>
> Beer(int n) : num(n) {}
>
> int getBeer(){
> return num;
> }
>
> void remove(){
> num--;
> }
>
> private:
> int num;
>
> };
>
> class Brew {
> public:
> /* Simple*/
> void drinkBeer(Beer b) {
> std::cout << "Drank " << b.getBeer() << " instances of beers!\n";
> b.remove();
> }
>
> void checkBeer(Beer b) {
> std::cout << b.getBeer() << " instances of beers left!\n";
> }
>
> /* With references. Notice that the '&' is placed AFTER the type like the
> * pointer operator. */
> void drinkBeerRef(Beer& b) {
> std::cout << "Drank " << b.getBeer() << " reference beers!\n";
> b.remove();
> }
>
> void checkBeerRef(Beer& b) {
> std::cout << b.getBeer() << " reference beers left!\n";
> }
>
> /* With pointers */
> void drinkBeer(Beer* b) {
> std::cout << "Drank " << b->getBeer() << " pointer beers!\n";
> b->remove();
> }
>
> void checkBeer(Beer* b) {
> std::cout << b->getBeer() << " pointer beers left!\n";
> }
>
> };
>
> int main(){
>
> /* (0) Calling default constructor. */
> Beer b1;
>
> /* (1) Make a brewery where you can drink! checkBeer will not print
> * the right value. */
> Beer b2(10); // make 10 beers.
> Brew brew;
> brew.drinkBeer(b2);
> brew.checkBeer(b2);


**NO NO NO**
the 2 later lines are syntax errors.You must use the address-of
operator (&) to extract the address of an object and pass it to a
pointer:

brew.drinkBeer(&b2);
brew.checkBeer(&b2);

>
> std::cout << std::endl;
>
> /* (2) Using references */
> Beer b3(10); // make 10 beers.
> brew.drinkBeerRef(b3);
> brew.checkBeerRef(b3);
>
> std::cout << std::endl;
>
> /* (3) Using pointers */
>
> /* Single object containing 10 beers. */
> Beer* b4 = new Beer(10);
>
> /* or:
> *
> * Beer* b4 = new Beer[10];
> *
> * using default constructor. 10 objects containing 1 beer.
> *
> * */
>
> brew.drinkBeer(b4);
> brew.checkBeer(b4);

you can dereference a pointer via the derefrence operator(unary *).try
this one too:

brew.drinkBeerRef( * b4 );
brew.checkBeerRef( * b4 );

beware: you need to get rid of dynamic objects(created via new/new[]
operators) before ending the program :

delete b4;
/* or:
*
* delete[] b4;Beer* b4 = new Beer[10];
*
* if Beer* b4 = new Beer[10];
*
*/
return 0;
>
> }
>
> How does passing an argument as a reference differ from passing it as a
> pointer (besides from also giving the right result)?
>
> When you use 'new' the data is allocated on the heap and when changed in
> a function will remain changed after the functions returns. But this is
> also the case with references...does 'Beer b3' allocate 'b3' on the heap?


No,'b3' is allocated on the stack and deallocated(first destructed
then deallocated) prior to exiting the enclosing code block(main
function in this context).

Tip:
Dynamic objects (created with new ) are placed on the heap and live
there unless you do kill them (destroy with delete).
None - static objects declared in code blocks are destroyed when the
enclosing block exits.
Instance data members are destroyed by the owner object.
static objects are destroyed at the end of the program.
pointers can be inc/decremented(++/--) like integral types.this is
usefull when working with intrinsic arrays:

int a[10];
int * ptr =a;//ptr = & a[0];
cout <<*++ptr;//ptr= & a[1];cout<<*ptr;

regards,
FM.



 
Reply With Quote
 
Chris Thomasson
Guest
Posts: n/a
 
      09-05-2007
"Carlo Capelli" <(E-Mail Removed)> wrote in message
news:ZbtDi.6287$(E-Mail Removed)...
[...]
> You can think to references as 'syntax sugar' for const pointers


Indeed.


 
Reply With Quote
 
desktop
Guest
Posts: n/a
 
      09-05-2007
terminator wrote:
> On Sep 5, 9:54 am, desktop <(E-Mail Removed)> wrote:
>> I have made this example:
>>
>> #include<iostream>
>>
>> class Beer {
>> public:
>> Beer() {
>> std::cout << "made a beer\n";
>> num = 1;
>> }
>>
>> Beer(int n) : num(n) {}
>>
>> int getBeer(){
>> return num;
>> }
>>
>> void remove(){
>> num--;
>> }
>>
>> private:
>> int num;
>>
>> };
>>
>> class Brew {
>> public:
>> /* Simple*/
>> void drinkBeer(Beer b) {
>> std::cout << "Drank " << b.getBeer() << " instances of beers!\n";
>> b.remove();
>> }
>>
>> void checkBeer(Beer b) {
>> std::cout << b.getBeer() << " instances of beers left!\n";
>> }
>>
>> /* With references. Notice that the '&' is placed AFTER the type like the
>> * pointer operator. */
>> void drinkBeerRef(Beer& b) {
>> std::cout << "Drank " << b.getBeer() << " reference beers!\n";
>> b.remove();
>> }
>>
>> void checkBeerRef(Beer& b) {
>> std::cout << b.getBeer() << " reference beers left!\n";
>> }
>>
>> /* With pointers */
>> void drinkBeer(Beer* b) {
>> std::cout << "Drank " << b->getBeer() << " pointer beers!\n";
>> b->remove();
>> }
>>
>> void checkBeer(Beer* b) {
>> std::cout << b->getBeer() << " pointer beers left!\n";
>> }
>>
>> };
>>
>> int main(){
>>
>> /* (0) Calling default constructor. */
>> Beer b1;
>>
>> /* (1) Make a brewery where you can drink! checkBeer will not print
>> * the right value. */
>> Beer b2(10); // make 10 beers.
>> Brew brew;
>> brew.drinkBeer(b2);
>> brew.checkBeer(b2);

>
> **NO NO NO**
> the 2 later lines are syntax errors.You must use the address-of
> operator (&) to extract the address of an object and pass it to a
> pointer:


Well I don't see why its syntactical incorrect. In Brew I have a method
that takes as argument a static object of type Beer.

I use the above lines to illustrate what happens if you DON'T use a
pointer or reference to Beer. The changes made will not be correct after
returning since the remove() call works on a copy of the passed object
and not the actual object.

To achieve the latter its necessary to pass the object as reference or
pointer.



>
> brew.drinkBeer(&b2);
> brew.checkBeer(&b2);
>
>> std::cout << std::endl;
>>
>> /* (2) Using references */
>> Beer b3(10); // make 10 beers.
>> brew.drinkBeerRef(b3);
>> brew.checkBeerRef(b3);
>>
>> std::cout << std::endl;
>>
>> /* (3) Using pointers */
>>
>> /* Single object containing 10 beers. */
>> Beer* b4 = new Beer(10);
>>
>> /* or:
>> *
>> * Beer* b4 = new Beer[10];
>> *
>> * using default constructor. 10 objects containing 1 beer.
>> *
>> * */
>>
>> brew.drinkBeer(b4);
>> brew.checkBeer(b4);

> you can dereference a pointer via the derefrence operator(unary *).try
> this one too:
>
> brew.drinkBeerRef( * b4 );
> brew.checkBeerRef( * b4 );
>
> beware: you need to get rid of dynamic objects(created via new/new[]
> operators) before ending the program :
>
> delete b4;
> /* or:
> *
> * delete[] b4;Beer* b4 = new Beer[10];
> *
> * if Beer* b4 = new Beer[10];
> *
> */
> return 0;
>> }
>>
>> How does passing an argument as a reference differ from passing it as a
>> pointer (besides from also giving the right result)?
>>
>> When you use 'new' the data is allocated on the heap and when changed in
>> a function will remain changed after the functions returns. But this is
>> also the case with references...does 'Beer b3' allocate 'b3' on the heap?

>
> No,'b3' is allocated on the stack and deallocated(first destructed
> then deallocated) prior to exiting the enclosing code block(main
> function in this context).
>
> Tip:
> Dynamic objects (created with new ) are placed on the heap and live
> there unless you do kill them (destroy with delete).
> None - static objects declared in code blocks are destroyed when the
> enclosing block exits.
> Instance data members are destroyed by the owner object.
> static objects are destroyed at the end of the program.
> pointers can be inc/decremented(++/--) like integral types.this is
> usefull when working with intrinsic arrays:



But a method that takes a reference to a static object will leave the
object changed after returning.


 
Reply With Quote
 
desktop
Guest
Posts: n/a
 
      09-05-2007
Carlo Capelli wrote:
> "desktop" <(E-Mail Removed)> ha scritto nel messaggio
> news:fbljr2$48m$(E-Mail Removed)-c.dk...
>> I have made this example:

> ....
>> How does passing an argument as a reference differ from passing it as a
>> pointer (besides from also giving the right result)?

>
> You can think to references as 'syntax sugar' for const pointers (beware,
> not pointer to consts).
> Passing an argument by reference is a safer and more convenient way to pass
> the pointer to the value.
> The callee can modify the value but not the pointer, i.e. can't switch the
> 'pointer' to something else.



Ok thats a very good explanation, thanks!


>> When you use 'new' the data is allocated on the heap and when changed in a
>> function will remain changed after the functions returns. But this is also
>> the case with references...does 'Beer b3' allocate 'b3' on the heap?

>
> b3 is on the stack, but the pertinent point is that the section marked /*
> Simple*/ it's incorrect.
> You pass 'by value' the beer, and that make sense only in the checkBeer
> call.


I know but the example is also made to show what happens if you DON'T
pass an object as pointer or reference.
 
Reply With Quote
 
terminator
Guest
Posts: n/a
 
      09-05-2007
On Sep 5, 2:41 pm, desktop <(E-Mail Removed)> wrote:
> terminator wrote:
> > On Sep 5, 9:54 am, desktop <(E-Mail Removed)> wrote:
> >> I have made this example:

>
> >> #include<iostream>

>
> >> class Beer {
> >> public:
> >> Beer() {
> >> std::cout << "made a beer\n";
> >> num = 1;
> >> }

>
> >> Beer(int n) : num(n) {}

>
> >> int getBeer(){
> >> return num;
> >> }

>
> >> void remove(){
> >> num--;
> >> }

>
> >> private:
> >> int num;

>
> >> };

>
> >> class Brew {
> >> public:
> >> /* Simple*/
> >> void drinkBeer(Beer b) {
> >> std::cout << "Drank " << b.getBeer() << " instances of beers!\n";
> >> b.remove();
> >> }

>
> >> void checkBeer(Beer b) {
> >> std::cout << b.getBeer() << " instances of beers left!\n";
> >> }

>
> >> /* With references. Notice that the '&' is placed AFTER the type like the
> >> * pointer operator. */
> >> void drinkBeerRef(Beer& b) {
> >> std::cout << "Drank " << b.getBeer() << " reference beers!\n";
> >> b.remove();
> >> }

>
> >> void checkBeerRef(Beer& b) {
> >> std::cout << b.getBeer() << " reference beers left!\n";
> >> }

>
> >> /* With pointers */
> >> void drinkBeer(Beer* b) {
> >> std::cout << "Drank " << b->getBeer() << " pointer beers!\n";
> >> b->remove();
> >> }

>
> >> void checkBeer(Beer* b) {
> >> std::cout << b->getBeer() << " pointer beers left!\n";
> >> }

>
> >> };

>
> >> int main(){

>
> >> /* (0) Calling default constructor. */
> >> Beer b1;

>
> >> /* (1) Make a brewery where you can drink! checkBeer will not print
> >> * the right value. */
> >> Beer b2(10); // make 10 beers.
> >> Brew brew;
> >> brew.drinkBeer(b2);
> >> brew.checkBeer(b2);

>
> > **NO NO NO**
> > the 2 later lines are syntax errors.You must use the address-of
> > operator (&) to extract the address of an object and pass it to a
> > pointer:

>
> Well I don't see why its syntactical incorrect. In Brew I have a method
> that takes as argument a static object of type Beer.
>
> I use the above lines to illustrate what happens if you DON'T use a
> pointer or reference to Beer. The changes made will not be correct after
> returning since the remove() call works on a copy of the passed object
> and not the actual object.
>
> To achieve the latter its necessary to pass the object as reference or
> pointer.
>
>
>
>
>
>
>
> > brew.drinkBeer(&b2);
> > brew.checkBeer(&b2);

>
> >> std::cout << std::endl;

>
> >> /* (2) Using references */
> >> Beer b3(10); // make 10 beers.
> >> brew.drinkBeerRef(b3);
> >> brew.checkBeerRef(b3);

>
> >> std::cout << std::endl;

>
> >> /* (3) Using pointers */

>
> >> /* Single object containing 10 beers. */
> >> Beer* b4 = new Beer(10);

>
> >> /* or:
> >> *
> >> * Beer* b4 = new Beer[10];
> >> *
> >> * using default constructor. 10 objects containing 1 beer.
> >> *
> >> * */

>
> >> brew.drinkBeer(b4);
> >> brew.checkBeer(b4);

> > you can dereference a pointer via the derefrence operator(unary *).try
> > this one too:

>
> > brew.drinkBeerRef( * b4 );
> > brew.checkBeerRef( * b4 );

>
> > beware: you need to get rid of dynamic objects(created via new/new[]
> > operators) before ending the program :

>
> > delete b4;
> > /* or:
> > *
> > * delete[] b4;Beer* b4 = new Beer[10];
> > *
> > * if Beer* b4 = new Beer[10];
> > *
> > */
> > return 0;
> >> }

>
> >> How does passing an argument as a reference differ from passing it as a
> >> pointer (besides from also giving the right result)?

>
> >> When you use 'new' the data is allocated on the heap and when changed in
> >> a function will remain changed after the functions returns. But this is
> >> also the case with references...does 'Beer b3' allocate 'b3' on the heap?

>
> > No,'b3' is allocated on the stack and deallocated(first destructed
> > then deallocated) prior to exiting the enclosing code block(main
> > function in this context).

>
> > Tip:
> > Dynamic objects (created with new ) are placed on the heap and live
> > there unless you do kill them (destroy with delete).
> > None - static objects declared in code blocks are destroyed when the
> > enclosing block exits.
> > Instance data members are destroyed by the owner object.
> > static objects are destroyed at the end of the program.
> > pointers can be inc/decremented(++/--) like integral types.this is
> > usefull when working with intrinsic arrays:

>
> But a method that takes a reference to a static object will leave the
> object changed after returning.- Hide quoted text -


pointers and refrences are both used for passing by reference but they
obey different symantics and have different syntaxes you can not
initialize a pointer with a normal variable or vice-versa:

class A{};
A a1
A * Aptr=a1;//error
A a2=Aptr;//error
A & Aref1=Aptr;//error

you must write:

A * Aptr= & a1;//pass the address of 'a1' to 'Aptr'
A a2= * Aptr ;//pass the value pointed by 'Aptr' to 'a2'
A & Aref1= a1;//make 'Aref1' an aliase(new name)for 'a1'
A & Aref2= *Aptr;//make 'Aref2' a reference to what 'Aptr' points to

you seem not to know the meaning of 'static';read your book before
asking any more Qs.

FM.


 
Reply With Quote
 
SasQ
Guest
Posts: n/a
 
      09-05-2007
On Wed, 05 Sep 2007 02:46:08 -0700, Chris Thomasson wrote:

>> You can think to references as 'syntax sugar' for
>> const pointers

>
> Indeed.


Not always true.
The compiler is able to "inline" access by reference
so that it access the original value directly.

--
SasQ
 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      09-05-2007
On Sep 5, 9:46 pm, "Chris Thomasson" <(E-Mail Removed)> wrote:
> "Carlo Capelli" <(E-Mail Removed)> wrote in message
>
> > You can think to references as 'syntax sugar' for const pointers

>
> Indeed.


If you like fostering misconceptions about the language, that is.

 
Reply With Quote
 
Chris Thomasson
Guest
Posts: n/a
 
      09-06-2007
"Old Wolf" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> On Sep 5, 9:46 pm, "Chris Thomasson" <(E-Mail Removed)> wrote:
>> "Carlo Capelli" <(E-Mail Removed)> wrote in message
>>
>> > You can think to references as 'syntax sugar' for const pointers

>>
>> Indeed.

>
> If you like fostering misconceptions about the language, that is.


Okay: I am wrong.

 
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
Snake references just as ok as Monty Python jokes/references in python community? :) seberino@spawar.navy.mil Python 8 12-12-2006 11:21 PM
Typedef A references struct B which references struct A which... DanielEKFA C++ 8 05-16-2005 10:26 AM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
how to understand references to variables and references to constants are distinguished? baumann.Pan@gmail.com C++ 3 11-10-2004 04:16 AM
Pointers and References (and References to Pointers) Roger Leigh C++ 8 11-17-2003 10:14 AM



Advertisments