Print Page | Close Window

Anybody Know C++???

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: General Discussion
Forum Description: General discussion and chat on any topic.
URL: https://forums.webwiz.net/forum_posts.asp?TID=15818
Printed Date: 31 March 2026 at 1:18pm
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Anybody Know C++???
Posted By: pmormr
Subject: Anybody Know C++???
Date 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*

-------------
Paul A Morgan

http://www.pmorganphoto.com/" rel="nofollow - http://www.pmorganphoto.com/



Replies:
Posted By: pmormr
Date 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

-------------
Paul A Morgan

http://www.pmorganphoto.com/" rel="nofollow - http://www.pmorganphoto.com/


Posted By: Mart
Date 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 .)


Posted By: dpyers
Date 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.


Posted By: pmormr
Date 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

-------------
Paul A Morgan

http://www.pmorganphoto.com/" rel="nofollow - http://www.pmorganphoto.com/


Posted By: Mart
Date 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?


Posted By: dpyers
Date Posted: 13 July 2005 at 3:42pm
This guy explains thing better than me
http://www.codeproject.com/cpp/pointers.asp - http://www.codeproject.com/cpp/pointers.asp


-------------

Lead me not into temptation... I know the short cut, follow me.


Posted By: pmormr
Date Posted: 13 July 2005 at 11:18pm
why you would use them

-------------
Paul A Morgan

http://www.pmorganphoto.com/" rel="nofollow - http://www.pmorganphoto.com/



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net