Question about const reference

Hey.. can anyone explain me why doing:

const int& a = 8;

is possible?
what does it do? how can A refer to something that doesnt exist?
and why does it have to be CONST?

thanks in advance!
I think this is one of those "behind the scenes" things that the compiler does.

When compiled, a will just be replaced with 8. It would have to be const because otherwise it could be theoretically changed which doesn't make sense since there is no address to start with.

It's similar to just creating:
static const int a = 8;

Unless someone makes a pointer point to a, the compiler won't even bother allocating memory to a and will instead just do a direct replacement at compile time although I suppose that could be compiler/optimization selection specific.
Last edited on
Yea, your answer is right ;) just got the same answer from Stroustrop's book.
thank you.
Sweet, it's nice to have confirmation like that!
Topic archived. No new replies allowed.