This topic is 4560 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hi, alright i understand first of all what they do and everything, just im reading/learning from a book and suddendly cranked the difficulty up just too far whilst learning definin them for example: const class & function(const class & theclass) / that bit -[class &]-- their can't get head around. also if for example your doing a class and you have a member function declared like: Node * Insert(type * theData) thats a pointer being assigned not initialized. Node * class::insert(type * theData) how do you pass the data to this function, is it by the address eg: &theData ?? can you explain this, not what pointers do blah blah blah but the understanding of what's going on and why.

##### Share on other sites
Quote:
 Original post by dcukhi, alright i understand first of all what they do and everything, just im reading/learning from a book and suddendly cranked the difficulty up just too far whilst learning definin them for example:const class & function(const class & theclass) / that bit -[class &]-- their can't get head around.

& is a reference, references are like an alias to a variable. This function returns and takes a const reference, not a copy of the class. It's mostly done since it's more efficient (no need to copy the whole class).
Quote:
 also if for example your doing a class and you have a member function declared like:Node * Insert(type * theData)thats a pointer being assigned not initialized.Node * class::insert(type * theData) how do you pass the data to this function, is it by the address eg: &theData ?? can you explain this, not what pointers do blah blah blah but the understanding of what's going on and why.

If theData is allocated on the stack (e.g. type theData;) then you need to use the address of operator insert(&theData). If you allocated it on the heap (type * theData = new type(); )then you can pass the pointer insert(theData). Insert is expecting a pointer to memory, so that's what you have to give it. Hope that helps!

##### Share on other sites
I'll give you a limited answer.
const class& function (const class & theclass)//here the first class& is passing the pointer returned from the function (hopefully it's a dynamically allocated pointer)//the second class & is simply passing the address of the incoming variable.//for:Node * Insert(type * theData)Node * class::insert(type * theData)//yes you would pass a variable as &theClassVariable

##### Share on other sites
still not clear,

const class & function(const class & theclass)

why is their the need for two refernces symbols there i undertstand the last as your passing the object in increasing efficiency but
:the one after Class == ??

with pointers:
Node * Insert(type * theData)

thats a pointer being assigned not initialized.

Node * class::insert(type * theData)

your saying to pass correctly into insert is to class->insert(&theData), passing the address of the pointer into it.?

##### Share on other sites
Quote:
 Original post by dcukstill not clear,const class & function(const class & theclass)why is their the need for two refernces symbols there i undertstand the last as your passing the object in increasing efficiency but :the one after Class == ??

If function were to return the class by value (class function()) then it would have to make a copy of the class and return it. If you return by const reference (as above) you don't need to copy the class, and since it may be a large class it would take a while to copy.

Quote:
 with pointers:Node * Insert(type * theData)thats a pointer being assigned not initialized.Node * class::insert(type * theData)your saying to pass correctly into insert is to class->insert(&theData), passing the address of the pointer into it.?

Not the address of the pointer, the address of the object. All insert is expecting is a pointer to an object of 'type'. If you already have a pointer to that object, you can pass it without the address-of operator, since it's pointing to the memory. However, if you just created the object ( type theData ) then theData is not a pointer. So, to get the memory address of theData you use address-of theData (&theData). Insert is looking for a memory address containing an object 'type'.

##### Share on other sites
Unfortunately, '&' is quite confusing.

used outside of a declaration:
int   bar;int   *foo;&bar       returns the address of bar.*foo       returns the value of foo.foo=&bar;  returns the address of bar, and assigns it to foo.*foo=bar;  returns the value of foo, and replaces it with the value of bar.& is the opposite of * here.

Used in a declaration, '&' indicates a reference. A reference is an address to a variable without changing the variable's indirection. That is, it's still the 'value' level of the variable, but does not copy it:

int  &square(int &x){       x=x*x;      return(x);}int     main(){int     y=6;int     z;cout << "y before: " << y << "\n";z=square(y);cout << "y after: " << y << "\n";cout << "z:       " << z << "\n";}// results:y before: 6y after: 36z:       36

See? You just pass the variable 'as is', and work with it 'as is', but it effects the variable itself, not a copy of it.

##### Share on other sites
Aye, '&' used to throw me off as well

##### Share on other sites
i understand all of that, just the refernce before square, whats different about that then passing a parameter through function??

##### Share on other sites
Quote:
 Original post by dcuki understand all of that, just the refernce before square, whats different about that then passing a parameter through function??

consider this then:

#include <iostream>using namespace std;int     &square(int &x){        x=x*x;        return(x);}int     valsquare(int x){        x=x*x;        return(x);}int     main(){int     y=6;int     z;cout << "y before: " << y << "\n";z=square(y);cout << "y after: " << y << "\n";cout << "z:       " << z << "\n";y=6;cout << "y before: " << y << "\n";z=valsquare(y);cout << "y after: " << y << "\n";cout << "z:       " << z << "\n";}

Note that square, and valsquare do the same exact thing.

results:
y before: 6y after: 36z:       36y before: 6y after: 6z:       36

##### Share on other sites
the reference before square allows the function to return a pointer.

very similar to:

type * functionname ()
type & functionname ()

##### Share on other sites
no but that can be sorted by simply putting a & before in the parameter for that function, what im curious about is the fact their is a reference before square:

&square

##### Share on other sites
Quote:
 Original post by dcukno but that can be sorted by simply putting a & before in the parameter for that function, what im curious about is the fact their is a reference before square:&square

oh. To my knowledge, that should do the same thing as simply returning a value, except it's faster for 'big' types [like classes] due to how it's handled by the compiler.

Not certain about that though. I've never found that distinction important enough to learn with certainty.

##### Share on other sites
The reference before square (it was int &square() right?) means a reference to an integer (int& square == int &square). This means that the return value of square() will be a reference to an integer. When you return a reference, if you modify that variable the original value will change as well.

so if you have:
int &square(int &x){x =x*x;return x;}y = 10;int& z = square(y); //y and z now equal 100z = 50; //y and z are now equal to 50

If square() returned a value then z = 50 would change the value of z only.

##### Share on other sites
ok so your saying that from a refernce before the function, that the return would be a refence also, separate from the one passed by parameter???

really have been confused by this

##### Share on other sites
Quote:
 Original post by dcukok so your saying that from a refernce before the function, that the return would be a refence also, separate from the one passed by parameter??? really have been confused by this

yes, the type before the function is the return value [pointer, reference, or value... wahtever], which is always independant from the parameters.

##### Share on other sites
Yes. The reference after the return type specifies that the function returns a reference. The reference after the parameter means the function takes a parameter as a reference. The two are totally independant from each other.
int &square(int x); //Function RETURNS an integer REFERENCE, TAKES an integer VALUEint square(int& x); //Function RETURNS an integer VALUE, TAKES an integer REFERENCEint &square(int& x); //Function RETURNS an integer REFERENCE, TAKES an integer REFERENCEint square(int x); //Function RETURNS an integer VALUE, TAKES an integer VALUE

Hope that's helpful, the whole reference/value business can be confusing at first but once you get it it's quite simple :D

##### Share on other sites
Quote:
 Original post by perfectly_darkThe reference before square (it was int &square() right?) means a reference to an integer (int& square == int &square). This means that the return value of square() will be a reference to an integer. When you return a reference, if you modify that variable the original value will change as well.so if you have:int &square(int &x){x =x*x;return x;}y = 10;int& z = square(y); //y and z now equal 100z = 50; //y and z are now equal to 50If square() returned a value then z = 50 would change the value of z only.

You know... I didn't know that. Thanks++.

##### Share on other sites
Be careful returning references. Specifically, don't return references to values allocated and destroyed within the function, as they won't exist by the time the function returns - meaning you return garbage. Generally, compilers will warn you about this, and many even treat it as an error and will fail to compile:
int & square(const int & x){  int sq = x * x;  return sq;  // BOOM! sq won't survive the function return}

Simply keep in mind that a reference is like a compiler-managed pointer (an address, not a variable) and you should be fine. Some people then go and do something like this, though:
int & square(const int & x){  int * sq = new int;  *sq = x * x;  return *sq;  // dynamically allocated, so it survives function return}

While the above will compile and execute properly, you're now left with really messy memory management, as the code that calls square now has to remember to deallocate the return value. This, ladies and gentlemen, is why smart pointers were invented. Use them (and especially boost::shared_ptr and other policy-based smart pointers).