Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it impossible to convert const X to X &?

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.

like image 608
Tanguy Labrador Ruiz Avatar asked May 04 '17 07:05

Tanguy Labrador Ruiz


People also ask

Why const correctness?

The benefit of const correctness is that it prevents you from inadvertently modifying something you didn't expect would be modified.

What does const& mean in C++?

The const keyword specifies that a variable's value is constant and tells the compiler to prevent the programmer from modifying it.

Does C have const correctness?

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.

What are const member functions?

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.


2 Answers

Why is it impossible to convert const X to X &?

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 without const ?

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).

like image 55
songyuanyao Avatar answered Oct 19 '22 15:10

songyuanyao


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;
}
like image 30
TartanLlama Avatar answered Oct 19 '22 16:10

TartanLlama