Your hi
method is not declared as const
inside your A class. Hence, the compiler cannot guarantee that calling a.hi()
will not change your constant reference to a
, thus it raises an error.
You can read more about constant member functions here and correct usage of the const
keyword here.