made me understand! advanced pointers references
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.
Quote:Original post by dcuk
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.
& 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!
I'll give you a limited answer.
i hope i partially answered your question
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
i hope i partially answered your question
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.?
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.?
Quote:Original post by dcuk
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 == ??
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'.
Unfortunately, '&' is quite confusing.
used outside of a declaration:
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:
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.
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.
i understand all of that, just the refernce before square, whats different about that then passing a parameter through function??
Quote:Original post by dcuk
i 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
the reference before square allows the function to return a pointer.
very similar to:
type * functionname ()
type & functionname ()
very similar to:
type * functionname ()
type & functionname ()
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement