So I am attempting to learn C++ and I have come across something that puzzles me slightly. I have the code,
int x = 0;
int &y = x;
cout << &x<< " " << x << " " << &y << " " <<y<< endl;
This compiles fine and results in:
0 003AFA08 0 003AFA08
What I have trouble understanding why the conversion of x, an int, to &y, a reference, doesn't result in an error. At first I thought it was some sort of conversion however,
int &y = &x;
results in an error.
Can anyone explain why this works in this way? Thanks in advance.
int& is not an address. It is a reference.
int& y = x; declares y as a reference to x. It effectively means that y becomes another name for x. Any time you use y, it is as if you had said x.
Pointers (not references) are used to store addresses. int& y = &x; is not valid because &x is the address of x (it's an int*). y is a reference to an int, not a reference to an int*.
It isn't a conversion. When you have a variable type of T & where T is some random type, you are basically saying "I'm declaring a name which is an alias for another name or possibly an anonymous value.". It's more like a typedef than a pointer.
References happen to often be implemented as addresses, but that isn't a good model for thinking about what they are.
In your example that you're puzzled by:
int * const &y = &x;
would work just fine. Then y becomes an alias for the temporary result of taking the address of x. Notice that it is a reference to a pointer. It has to be a reference to a constant pointer because it is a reference to a temporary value.
Related
why is it possible to change the value of the constant z in the C++ snippet below? What is the meaning of (int&) in the 5th line? Note that the addresses of x, y, z are different (cout << &x << &y << &z). Thank you very much!
int x = 2;
int y = 3;
const int z = x;
cout << z; // the result is 2
(int&) z = y;
cout << z; // the result is 3
Because in this case we have a cast on the left side of an assignment expression that tells to compiler we are forcing our identifier in order it behaves as a not read-only reference to its memory location, and since we are knowingly "forcing" a behaviour, compiler trust us and allows this assignment.
In some cases this is potentially dangerous, as when you assign the const identifier with a literal value, because it is not standard how compilers implement this initialization, in fact some compilers seems that replace the occurrences of identifiers with the literal values, as well as a macro expansion, so you have to pay a very close attention doing this operation.
I am reading through Stroustrup's 4th edition : The C++ Programming Language. I have a python/java background so the first 4 chapters are fine so far.
In Chapter 3 I saw:
complex& operator+=(complex z) { re+=z.re , im+=z.im; return ∗this; }
That began a day long attempt to write this question:
First I figured out that it is returning a reference to the object and not a copy. As I was able to confirm in this question.
And I was able to understand the difference between returning a reference into a reference variable vs. a regular variable from this question
And I did my own trial
class Test {
public:
Test():x{5}{}
int x;
void setX(int a) {x = a;}
Test& operator+=(Test z) {x+=z.x; return *this;}
// the keyword this is a pointer
Test* getTest() {return this;}
// but I can return the reference by *this
Test& getTest1() {return *this;}
// or I can return a copy
Test getTest2() {return *this;}
};
That lead me to question why it is called de-reference, so I did this trial
int x = 8;
int* p = &x;
int y = *p;
int& z = *p;
x++; // let's have some fun
std::cout << y << std::endl;
std::cout << z << std::endl;
As expected y = 8 and z = 9, so how did the de-reference return the address in one case, and the value in the other? More importantly how is C++ making that distinction?
It's exactly like in your Test class functions.
int y = *p;
int& z = *p;
y is a copy of what p points to.
z is a reference to (not an address) what p points to. So changing z changes *p and vice-versa. But changing y has no effect on *p.
As expected y = 8 and z = 9, so how did the de-reference return the address in one case, and the value in the other? More importantly how is C++ making that distinction?
The de-reference returned the actual thing referenced in both cases. So there is no distinction for C++ to make. The difference is in what was done with the result of the dereference.
If you do int j = <something>; then the result of the something is used to initialize j. Since j is an integer, the <something> must be an integer value.
If you do int &j = <something>; then the result of the something is still used to initialize j. But now, j is a reference to an integer, and the <something> must be an integer, not just an integer value.
So, what *this does is the same in both cases. How you use a value doesn't affect how that value is computed. But how you use it does affect what happens when you use it. And these two pieces of code use the dereferenced object differently. In one case, its value is taken. In the other case, a reference is bound to it.
It's possible to consider a pointer int* p as pointing to an address where data of type int resides. When you de-reference this, the system retrieves the value at that memory address (the address is the actual value of p itself). In the case of int y = *p; you put a copy of that int value on the stack as the locator value y.
On the other hand, de-referencing on the left-hand side in *p = 13; means you are replacing the int value *p stored at the memory address denoted by the value of p with the right-hand-side value 13.
The reference lvalue int& z in int& z = *p; is not a copy of the int value pointed to by p but rather a left-hand side reference to whatever is at the particular memory address returned by *p (i.e. the actual value held by p itself).
This doesn't mean much difference in your contrived case, but e.g. given a Foo class with a Foo::incrementCount() value,
Foo* p = new Foo();
p->incrementCount();
Foo& ref = *p;
ref.incrementCount();
The same method for the same instance will be called twice. In contrast, Foo foo = *p will actually copy the entire Foo instance, creating a separate copy on the stack. Thus, calling foo.incrementValue() won't affect the separate object still pointed to by p.
This question already has answers here:
Temporary initialization and Reference initialization
(3 answers)
Closed 6 months ago.
I am trying to understand constant reference in C++ and I stumbled across this problem:
When I assign double to a const int& and then change the value of the referencing double, the value of my int reference stays constant.
double i = 10;
const int &ref = i;
i = 20;
cout << "i: " << i << endl; // i = 20
cout << "&ref: " << ref << endl; // ref = 10
Whereas while assigning int, the value gets changed.
int i = 10;
const int &ref = i;
i = 20;
cout << "i: " << i << endl; // i = 20
cout << "&ref: " << ref << endl; // ref = 20
What is the reason for this behaviour? My guess is that when assigning double, the implicit conversion to int creates a new object and its reference is then being assigned, however I can't find it written down anywhere.
Even though it looks like, ref isn't a reference to i.
double i = 10;
const int &ref = i; // ***
i = 20;
A reference to an int cannot refer to a double. Therefore, in the marked line, a conversion from a double to an int takes place and the result of conversion is a temporary rvalue. Normaly, they are destroyed when the full expression they were created in ends. But there's a rule in C++ that allows extending the lifetime of a temporary when it is bound to a reference to const. It is extended until the reference dies. For that, the compiler actually allocates some memory for it behind the scenes.
The assignment modifies the original object i, the unnamed temporary stays intact.
When you convert the double to an int, the result is an rvalue of type int. This temporary value is bound to a constant reference variable, and thus the lifetime of the temporary value is extended to that of the reference variable. Changing the original double has no effect on that.
The compiler can help you a lot only if you remove const from both declarations and attempt to compile the code snippets:
double i = 10;
/*const*/ int &ref = i; //ERROR - WHY?
This code will give compilation error! Why?
But this code:
int i = 10;
/*const*/ int &ref = i; //NO ERROR
will compile fine.
If the first code compiles fine when there is const, why doesn't it compile now (when const is not there)? What is going on under the hood?
Well, the reason is, actually int& is a reference type which can refer to an object of type int only, not double, so a temporary object of type int is created out of the expressioni (which is double), but the temporary object cannot bind to int& , so it gives compilation error, but as soon as you put const there, it compiles fine, because a temporary object of type int can bind to const int&.
No such thing happens in the second code — no temporary object is created. So when you set i to 20, the ref also changes in the second case (as it refers to i) but in the first case, when you set i to 20, the ref does not change because ref does not refer to i anymore — it rather refers to the temporary object (which you're not changing!). That explains the behaviors of your code snippets.
Hope that helps.
double i = 10;
const int &ref = i;
Depending on the compiler you are using this code, as is, could as well give a compilation error.
On a compiler where it throws an error, if you cast it to (int &) you would see undefined behavior, since you are reading an int from a double's location.
On a compiler which accepts this line, my best guess is it is creating a temporary variable (an rvalue) and assigning to the int reference, and any changes to the original variable are not reflected in the reference.
For the reference to work make sure the source data type can be represented by destination data type.
You can think of an int as a class, and int(double); as a way of constructing an int from a double, you have a const reference to that int, which was constructed from a double.
I'm looking few exercise from university about C++ and I found out this exercise:
#include <iostream>
using namespace std;
int& f(int*&);
int& f(int*& x) {
*x = 5;
return *x;
}
int main() {
int y = 1, x;
int* z = &y;
x= f(z);
cout << y << " " << x <<endl;
}
I was wondering: does <any type>*& has any real sense? Isn't f(int*& x) the same as f(int x)? Aren't you passing the pointer to the L-value of the variable?
f(int*& x) is not the same as f(int x). In the first case x is a reference to an integer pointer whereas in the second case x is just an integer.
Lets start from the basics:
When you write f(int &x) means that x is a reference to an integer and you can change the value of x in the function and the change will be reflected in the calling function.
Similarly, when you write f(int*& x), it means that x is reference to an integer pointer and when you change the address that x points to, the change will also be reflected in the calling function.
With int* &x you are passing the same pointer(by reference). Otherwise with only int* x you are passing a copy of the pointer and then you can't change the original one in the function. &x makes x an alias of the original parameter.
It's a reference to a pointer to an int. The function is then able to change the pointer if it wants to. In your example it doesn't make sense, but it obviously does have a use.
nope: *& here doesn't mean "dereference addressof". It means: "pass a pointer byref".
int& f(int*& x) {
*x = 5; // note: changes the pointee, not the pointer
return *x;
}
In this example, you don't gain anything by passing the pointer by reference, since you're not changing the pointer. Passing a pointer by reference is only needed when you intend to change the pointer:
void f(int*& x) {
x = new int(42); // note: changes the pointer
}
I think you are confusing sybols when they are used as operators or declarators.
If you use * when declaring variable, that means veriable is a pointer. When you use * as operator, that is dereference operator.
int *& name
simply means you are taking pointer to int by reference. **The * and & do not cancel each other out.**
If you had line of code like this:
var = *& var2;
then yes, it would be same as:
var = var2;
I typed the following example:
#include <iostream>
double f(double* x, double* y)
{
std::cout << "val x: " << *x << "\n";
std::cout << "val y: " << *y << "\n";
return *x * *y;
}
double f2(double &x, double &y)
{
std::cout << "val x: " << x << "\n";
std::cout << "val y: " << y << "\n";
return x * y;
}
int main()
{
double a, b;
a = 2;
b = 3;
std::cout << f(&a, &b) << "\n";
std::cout << f2(a, b) << "\n";
return 0;
}
In the function f I declare x and y as pointers of which I can get the value by using *x. When calling f I need to pass the address of my passed arguments, that is why I pass &a, &b. f2 is the same except the definition is different.
Now my question is: Are they both really the same concerning memory management? Both not making any copy of the passed value but instead passing a reference?
I wonder about f2 because I couldn't read out the address of x in f2 so I know more about x and y in f (there I know address AND value).
Edit: After doing some more research, I found a quite useful topic:
Pointer vs. Reference
There's also a link to google coding guidelines http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Reference_Arguments which is quite useful I feel (as I understood now, it's a form of subject taste) to make more clear
f2 is taking it's arguments by reference, which is essentially an alias for the arguments you pass. The difference between pointer and reference is that a reference cannot be NULL. With the f you need to pass the address (using & operator) of the parameters you're passing to the pointer, where when you pass by reference you just pass the parameters and the alias is created.
Passing by const reference (const double& ref) is preferred when you are not going to change the arguments inside the function, and when you are going to change them, use non-const reference.
Pointers are mostly used when you need to be able to pass NULL to your parameters, obviously you'd need to check then inside your function if the pointer was not NULL before using it.
This is just syntactic sugar to avoid having to use * everytime you reference the argument. You still can use & to have the address of x in f2.
In my head, parameters of functions are always passed by value. Passing an int is easy to imagine, passing a double is just bigger and passing a struct or class could be very big indeed.
But passing a pointer to something, well, you're just passing an address by value. (A pointer is often a convenient size for the CPU just like an int.)
A reference is very similar, and certainly I think of a reference as a pointer, but with syntactic sugar to make it look like the object its referring to has been passed by value.
You can also think of a reference as a const pointer, ie:
int i;
int j;
int* p = &i; // pointer to i
int* const cp = p; // cp points to i, but cp cannot be modified
p = &j; // OK - p is modified to point to j
*cp = 0; // OK - i is overwritten
cp = &j; // ERROR - cp cannot be modified
int& ri = i; // ri refers to i
ri = 1; // i is overwritten
ri = j; // i is overwritten again
// Did you think ri might refer to j?
So, a pointer does double time: It is a value in its own right, but it can also point to another value when you dereference it, eg: *p.
Also, having reference parameters means that you cannot make them refer to anything else during the lifetime of the function because there's no way to express that.
A reference is supposed not to be able to be initialised with null, but consider this:
void foo(int& i);
int* p = 0;
foo(*p);
This means that pointers should be checked before you use them, but references cannot be checked. The implementation of foo() could try to read from or write to i which will cause an access violation.
In the above example the pointer p should have been checked before being used in the call to foo:
if (p) foo(*p);
Another difference that hasn't been mentioned is that you cannot change what a reference refers to. This doesn't make a lot of difference in the function call example shown in the original question.
int X(10), Y(20);
int *pX = X;
int& rY = Y;
*pX = 15; // change value of X
rY = 25; // change value of Y
pX = Y; // pX now points to Y
rY always points to Y and cannot be moved.
References can't be used to index into simple arrays like pointers.
You should have been able to read x address in both functions.
To do so in f2, you must of course prefix x by a & since there, x is a reference to a double, and you want an address.
A worth noticing difference between references and pointers is that the former cannot be NULL. You must pass something (valid) while when providing a pointer, you must specify in the documentation if passing NULL is allowed/well defined.
Another difference is a matter of readability: using references instead of pointers (when possible) makes the code less cluttered with * and ->.