I'm trying to understand and achieve const correctness on a Tetris Project.
Here is a recurrent problem that i have when i'm trying to add const where I think it's necessary.
I have a (Piece) class, and one of it's class private member is
Point rotationCenter;
And I'm trying to write a getter like this:
inline Point & Piece::getRotationCenter() const
{
return rotationCenter;
}
Before, I had the same getter, but not as a const function, and was working. Now, I got the C2240 error "impossible to convert const Point to Point &".
What should I do to correct this? Should I leave getRotationCenter
without const
?
PS : I read https://isocpp.org/wiki/faq/const-correctness as tutorial.
The benefit of const correctness is that it prevents you from inadvertently modifying something you didn't expect would be modified.
The const keyword specifies that a variable's value is constant and tells the compiler to prevent the programmer from modifying it.
In C, C++, and D, all data types, including those defined by the user, can be declared const , and const-correctness dictates that all variables or objects should be declared as such unless they need to be modified.
The const member functions are the functions which are declared as constant in the program. The object called by these functions cannot be modified. It is recommended to use const keyword so that accidental changes to object are avoided. A const member function can be called by any type of object.
Why is it impossible to convert
const X
toX &
?
Because if it is allowed, the following dangerous code becomes valid:
const int x = 0;
int& rx = x; // bind const variable to reference (to non-const)
rx = 99; // oops, try to modify the const variable via the reference
What should I do to correct this? Should I leave
getRotationCenter
withoutconst
?
It depends on your intent. If the returned object could be modified, then make the member function non-const and return Point&
. If not, then leave the member function const
and make the return type const Point&
. A const member function means a promise that won't modify (or provide the possibility to modify) the object (and its members).
Inside const
member functions all of the classes data members are const
. You cannot bind a non-const
reference to your const
member data, so you get a compiler error.
If you don't want your caller to modify rotationCenter
, then you can return by Point
or const Point&
.
inline const Point & Piece::getRotationCenter() const
{
return rotationCenter;
}
If you do want the caller to modify rotationCenter
(which I would generally not recommend), write two overloads: one which returns by Point&
and one which returns by const Point&
depending on the qualification of the object you call it on:
inline Point & Piece::getRotationCenter() //not const
{
return rotationCenter;
}
inline const Point & Piece::getRotationCenter() const
{
return rotationCenter;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With