0

In c++, a reference variable can refer to an object as below

int i;

int &ref = i;

BUT if we put "const" keyword it can take any value, may be some variable or constant value.

Could some one brief me on this?

Regards, Abhineet

juanchopanza
  • 223,364
  • 34
  • 402
  • 480

1 Answers1

1

This is known as const-correctness.

The standard does not define any implicit conversions from cv-qualified to less qualified types, and references act mostly like pointers which can only be assigned once and dereference automatically(Though you cannot bind a non-const lvalue to a non-const reference).

Still, you can use const_cast to override the compiler. On your own head be it then.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118