made me understand! advanced pointers references

Started by
16 comments, last by Oluseyi 18 years, 7 months ago
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

Advertisement
Quote:Original post by dcuk
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


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.
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.
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
Quote:Original post by dcuk
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


yes, the type before the function is the return value [pointer, reference, or value... wahtever], which is always independant from the parameters.
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
Quote:Original post by perfectly_dark
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.

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

Beginner in Game Development?  Read here. And read here.

 

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).

This topic is closed to new replies.

Advertisement