Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - Anybody Know C++???
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Anybody Know C++???

 Post Reply Post Reply
Author
pmormr View Drop Down
Senior Member
Senior Member


Joined: 06 January 2003
Location: United States
Status: Offline
Points: 1479
Post Options Post Options   Thanks (0) Thanks(0)   Quote pmormr Quote  Post ReplyReply Direct Link To This Post 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*
Back to Top
pmormr View Drop Down
Senior Member
Senior Member


Joined: 06 January 2003
Location: United States
Status: Offline
Points: 1479
Post Options Post Options   Thanks (0) Thanks(0)   Quote pmormr Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
Mart View Drop Down
Senior Member
Senior Member
Avatar

Joined: 30 November 2002
Status: Offline
Points: 2304
Post Options Post Options   Thanks (0) Thanks(0)   Quote Mart Quote  Post ReplyReply Direct Link To This Post 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 .)
Back to Top
dpyers View Drop Down
Senior Member
Senior Member


Joined: 12 May 2003
Status: Offline
Points: 3937
Post Options Post Options   Thanks (0) Thanks(0)   Quote dpyers Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
pmormr View Drop Down
Senior Member
Senior Member


Joined: 06 January 2003
Location: United States
Status: Offline
Points: 1479
Post Options Post Options   Thanks (0) Thanks(0)   Quote pmormr Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
Mart View Drop Down
Senior Member
Senior Member
Avatar

Joined: 30 November 2002
Status: Offline
Points: 2304
Post Options Post Options   Thanks (0) Thanks(0)   Quote Mart Quote  Post ReplyReply Direct Link To This Post 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?
Back to Top
dpyers View Drop Down
Senior Member
Senior Member


Joined: 12 May 2003
Status: Offline
Points: 3937
Post Options Post Options   Thanks (0) Thanks(0)   Quote dpyers Quote  Post ReplyReply Direct Link To This Post Posted: 13 July 2005 at 3:42pm
This guy explains thing better than me
http://www.codeproject.com/cpp/pointers.asp

Lead me not into temptation... I know the short cut, follow me.
Back to Top
pmormr View Drop Down
Senior Member
Senior Member


Joined: 06 January 2003
Location: United States
Status: Offline
Points: 1479
Post Options Post Options   Thanks (0) Thanks(0)   Quote pmormr Quote  Post ReplyReply Direct Link To This Post Posted: 13 July 2005 at 11:18pm
why you would use them
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.