Jump to content
  • Advertisement
Sign in to follow this  
dcuk

made me understand! advanced pointers references

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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!

Share this post


Link to post
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


i hope i partially answered your question

Share this post


Link to post
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 this post


Link to post
Share on other sites
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'.

Share this post


Link to post
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: 6
y after: 36
z: 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 this post


Link to post
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 this post


Link to post
Share on other sites
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: 6
y after: 36
z: 36
y before: 6
y after: 6
z: 36

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!