While studying constants, pointers and references, I encountered the following problem. The following code throws an error: cannot bind non-const lvalue reference of type ‘const int*&’ to an rvalue of type ‘const int*’
.
int* numPtr = new int(5);
const int*& numPtrRef = numPtr;
But the following 2 blocks of code work without errors.
int* numPtr = new int(5);
const int* const& numPtrRef = numPtr;
const int* numPtr = new int(5);
const int*& numPtrRef = numPtr;
I don't understand why this is happening. It seems like non-const objects can be assigned to const objects, but for some reason the error seems to say the opposite. Please help me figure this out.
I do not have any practical goal, I want to understand the c ++ language, so I'm wondering why this code gives an error.