Hi i am reading about auto and const in C++ and came to know about 2 examples. The 2 examples are as follows:
auto &h = 42;
const auto &j = 42;
Now my questions are as follows:
- Why in the 1st example a plain reference cannot be bind to a literal and this example fails. I think it is because a reference is itself not an object it is just another name for an object and since the literal 42 does not exist as an object in memory so the 1st example fails. Is this correct?
- How does the second example works? I mean since a reference is itself not an object but since the 2nd example is working does this mean that the reference variable j is created as an object?
- So if the 2nd example is working does this mean in future if i want to create a variable using
int i=0;
instead can i create it usingconst auto &i=0
? Will this have the same affect as int i=0?
Aucun commentaire:
Enregistrer un commentaire