Re: References or pointers?
On Sep 5, 9:54 am, desktop <f...@sss.com> 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.