| Author |
Topic Search Topic Options
|
pmormr
Senior Member
Joined: 06 January 2003
Location: United States
Status: Offline
Points: 1479
|
Post Options
Thanks(0)
Quote Reply
Topic: Anybody Know C++??? Posted: 13 July 2005 at 2:18am |
|
I know most of us use VB or whatnot.. but I'm desperate. I've been trying to learn C++ and I have a few questions... if anybody can help clear these up i'll be eternally grateful!
Deals with: Returning Pointers and References
I know when you want to return a pointer you declare the function like:
int * function()
and when you want to return a reference you use
int & function()
but i have no f***ing clue when you would actually use them. The book i'm using introduces the whole concept using complicated objects with copy constructors and never explained the whole concept of returning pointers and references. I'm assuming that it would only be meaningful to return references and pointers to objects... but i need some really basic examples of each using integers or something like that... especially returning references.. why would you ever return a reference? From what i understand, if you create a value in a function and reference it... it would just fall out of scope and the reference would be meaningless because it would point to unallocated memory.
Aargh! Help. Confused. *Crunches bottle of asprin* *calls hospital because ODed on Asprin*
|
|
|
 |
pmormr
Senior Member
Joined: 06 January 2003
Location: United States
Status: Offline
Points: 1479
|
Post Options
Thanks(0)
Quote Reply
Posted: 13 July 2005 at 2:54am |
shoot... pushed this post to the bottom when i posted in like 20 threads... this is spam... just so you know
|
|
|
 |
Mart
Senior Member
Joined: 30 November 2002
Status: Offline
Points: 2304
|
Post Options
Thanks(0)
Quote Reply
Posted: 13 July 2005 at 4:47am |
|
I'm not sure if you mean pointers in general or pointers to functions.
Basically the reason you would want to pass a value by reference is so
that you can return/manipulte multiple objects at the same time...
If you don't pass by reference you pass by value - which means more
memory is allocated for the variable you are passing and the current
value is copied over there.
That means that if you've passed by value and you change the value of
the variable in your function, when your function goes out of scope the
variable will have the original value... probably best explaing that by
code...
bankaccount.h :
class bankaccount
{
protected:
float balance;
public:
void withdraw(float amount);
void deposit(float amount);
void transfer(bankaccount *source, bankaccount *destination, float
amount);
float getBalance();
bankaccount();
};
bankaccount::bankaccount()
{
balance = 0;
}
void bankaccount::withdraw(float amount)
{
std::cout << "Withdrawing " << amount;
balance -= amount;
}
void bankaccount::deposit(float amount)
{
balance += amount;
}
&n bsp;
void bankaccount::transfer(bankaccount *source, bankaccount *destination, float amount)
{
source->withdraw(amount);
destination->deposit(amount);
}
float bankaccount::getBalance()
{
return balance;
}
main.cpp:
#include <iostream>
#include <string>
#include "bankaccount.h"
using namespace std;
int main()
{
bankaccount account1;
bankaccount account2;
cout << account1.getBalance() << endl;
account1.deposit(100);
cout << account1.getBalance() << endl;
account2.transfer(&account1, &account2, 100);
cout << account1.getBalance() << endl;
cout << account2.getBalance() << endl;
system("PAUSE");
return 0;
}
The output from that code is:
0
100
Withdrawing 1000
100
Press any key to continue . . .
Now if you change that code not to use pointers you should see why you
would want to use them. (Get rid of all the & and *'s and replace
-> with .)
|
 |
dpyers
Senior Member
Joined: 12 May 2003
Status: Offline
Points: 3937
|
Post Options
Thanks(0)
Quote Reply
Posted: 13 July 2005 at 5:14am |
|
I always think of them as...
A pointer points to a memory location that contains the value. It
provides access to a place that contains a value. Pointers must be the
same type as the object they refer to.
A reference is an alias of a pointer (a pointer to a pointer) but you
can directly equate it to the value. A reference is actually an alias
of the value contained in the memory location - not an alias of the
memory address.
You can reassign a pointer to a different memory location containing a
different value but you can't reassign a reference to a different
memory location.
|
Lead me not into temptation... I know the short cut, follow me.
|
 |
pmormr
Senior Member
Joined: 06 January 2003
Location: United States
Status: Offline
Points: 1479
|
Post Options
Thanks(0)
Quote Reply
Posted: 13 July 2005 at 2:34pm |
|
my entire confusion deals with the * and & in the function declaration i.e.
int * MyFunction() //returns a pointer to an integer... buy why?
int & MyFunction() //returns a reference to an integer... but why?
thanks a lot guys
|
|
|
 |
Mart
Senior Member
Joined: 30 November 2002
Status: Offline
Points: 2304
|
Post Options
Thanks(0)
Quote Reply
Posted: 13 July 2005 at 2:37pm |
|
I still don't quite get what you're asking... do you want to know why
it returns a reference or a pointer, or why would you want to use them?
|
 |
dpyers
Senior Member
Joined: 12 May 2003
Status: Offline
Points: 3937
|
Post Options
Thanks(0)
Quote Reply
Posted: 13 July 2005 at 3:42pm |
|
|
Lead me not into temptation... I know the short cut, follow me.
|
 |
pmormr
Senior Member
Joined: 06 January 2003
Location: United States
Status: Offline
Points: 1479
|
Post Options
Thanks(0)
Quote Reply
Posted: 13 July 2005 at 11:18pm |
|
why you would use them
|
|
|
 |