Following are a simple C++ struct and a class for 3D vector and 3x3 matrix. As you can see the subscript operator is overloaded for both of them. I return a reference to the data members from them in order to be able to assign to those members like vec[0] = 5.0;
, mat[0] = vec3(2.0);
and mat[0][0] = 3.0;
etc. Now I declare another operator for mat3
, the plus(+) operator which should not modify the matrix itself but should return a new matrix as the sum. How can I make this non-modifying + operator as const
?
struct vec3 {
typedef double value_type;
vec3::value_type x, y, z;
vec3(const vec3::value_type x,
const vec3::value_type y,
const vec3::value_type z)
: x{x}, y{y}, z{z} {}
vec3(const vec3::value_type w)
: vec3(w, w, w) {}
vec3()
: vec3(0.0) {}
// this works because [] operator is not used
vec3 operator+(const vec3& v) const {
return vec3(this->x + v.x, this->y + v.y, this->z + v.z);
}
vec3::value_type& operator[](const std::size_t index) {
switch(index) {
case 0:
return x;
case 1:
return y;
case 2:
return z;
default:
throw std::invalid_argument("vec3 supports upto 3(0-2) elements");
}
}
};
class mat3 {
std::array<vec3,3> val;
public:
using vtype = vec3::value_type;
mat3(const vtype v00, const vtype v01, const vtype v02,
const vtype v10, const vtype v11, const vtype v12,
const vtype v20, const vtype v21, const vtype v22) {
val[0][0] = v00; val[0][1] = v10; val[0][2] = v20;
val[1][0] = v01; val[1][1] = v11; val[1][2] = v21;
val[2][0] = v02; val[2][1] = v21; val[2][2] = v22;
}
mat3(const vtype m[3][3]) {
for(std::size_t i = 0; i < 3; ++i) {
for(std::size_t j = 0; j < 3; ++j) {
val[i][j] = m[j][i];
}
}
}
mat3(const vtype v)
: mat3(v, v, v,
v, v, v,
v, v, v) {}
mat3()
: mat3(0.0) {}
// how to make it `const`
mat3 operator+(const mat3& m) {
mat3 t;
for(std::size_t i = 0; i < 3; ++i) {
t[i] = val[i] + m[i];
}
return std::move(t);
}
vec3& operator[](const std::size_t index) {
switch(index) {
case 0:
return val[0];
case 1:
return val[1];
case 2:
return val[2];
default:
throw std::invalid_argument("mat3 supports upto 3(0-2) vec3");
}
}
};
You could (and should) add a const version for operator[]
, then you can make operator+
const, the const version of operator[]
will be invoked in it.
vec3& operator[](const std::size_t index) {
...
}
const vec3& operator[](const std::size_t index) const {
~~~~~ ~~~~~
...
}
Note you can overload const and non-const member functions.
See const-, volatile-, and ref-qualified member functions (emphasis mine)
A non-static member function can be declared with a
const
,volatile
, orconst
volatile
qualifier (this qualifier appears after the name of the function in function declaration). Differently cv-qualified functions have different types and so may overload each other.In the body of a cv-qualified function, the
this
pointer is cv-qualified, e.g. in a const member function, only other const member functions may be called normally. (A non-const member function may still be called ifconst_cast
is applied or through an access path that does not involvethis
.)