c++ – error: passing const … as this argument of … discards qualifiers
c++ – error: passing const … as this argument of … discards qualifiers
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.
- As already mentioned, one option is to make
hi
method const-qualified. - Another option is to use const_cast at the time of calling the
hi
method like so
A& ref = const_cast <A&>(a); ref.hi();
c++ – error: passing const … as this argument of … discards qualifiers
Related posts on c++ :
- c++ – QWebSocket Hello World Example
- string – What is the meaning of this C++ Error std::length_error
- c++ – VS2012 MSVCR120D.dll is missing
- c++ – InterlockedIncrement usage
- visual c++ – filling up an array in c++
- c++ – Eigen – get a matrix from a map?
- c++ – no default constructor exists for class
- c++ error: invalid types int[int] for array subscript