I want to create a simple 3x3 matrix class and access its contents with an index operator. Here is the code:
class Matrix {
private:
int matrix[3][3];
public:
int* operator[](const int index) const;
};
int* Matrix::operator[](const int index) const {
return this->matrix[index];
}
I want to have access to the elements of the array, regardless of whether the Matrix object is const or not const. But I get the following error from the compiler:
Error: Incorrect conversion from 'const int *' to 'int *' [-fpermissive]
I did some research, and I have a hypothesis: maybe because I declared this member function as a const function, inside its definition, the compiler treats (it masks) all objects not changed by the user as constant members, so this will be the reason that the compiler is talking about an invalid conversion from 'const int *' to 'int *'. My question is: is this hypothesis correct? And if this is not so, why is this happening? I think this makes sense and would be a great way to provide the const constrix * this object constant.
Compiler Information: gcc 5.3.0 download from .com equation
source
share