| Thank you very much for your detail explanation.
|
| But if
| /*
| &n = 006BFDD8
| &r = 006BFDD8
| &rr = 006BFDD8
|
| Does it means that n, r and rr occupy the same memory space, since
| their addresses are the same?
No, &r is not returning the address of the reference itself, its
returning the adress of the referred-to variable. You can't directly
measure the size of the reference itself either since sizeof(r) returns
the type size of the referred-to variable.
While the reference itself does occupy memory (usually the same size as
a pointer) its job is to serve the referred-to object. The 12 bytes on
this platform indirectly exposes the actual size. But the reference is
not allowed to return that value. If it did, it would break its purpose.
|
| But sizeof(var) = 12 // an integer(4 bytes) + 2 references(4 Bytes
| each).
| It seems to me that they are three different integer variables. I am
| confused.
|
| Thanks.
|
Its one integer and two pointers-on-steroids. You are confused because
on a typical 32 bit platform an integer happens to occupy the same space
in memory as a pointer. ok, try...
#include <iostream>
#include <ostream>
class N
{
double d;
double& r;
double& rr;
public:
N(double d_) : d(d_), r(d), rr(d) { }
~N() { }
/* friend op<< */
friend std:

stream& operator<<(std:

stream& os, const N& ref)
{
os << ref.d;
return os;
}
};
int main()
{
N n(11.1);
std::cout << " n = " << n; // op<<
std::cout << "\n sizeof(double) = " << sizeof(double);
std::cout << "\n sizeof(var) = " << sizeof(n);
std::cout << std::endl;
return 0;
}
/*
n = 11.1
sizeof(double) = 8
sizeof(var) = 16
*/