DoubleOseven DoubleOseven - 5 months ago 73
C++ Question

c++ overloading operator stack overflow

I am trying to overload the

operator. I am getting an unhandled exception error. I am implementing a matrix class using a

double& Matrix::operator[](int i)
return (*this)[i];


(*this) returns your Matrix object. Therefore, (*this)[i] calls Matrix::operator[](i). Your function is calling itself without any terminating conditions, similar to this:

int foo() {
    return foo();

Which, as you can imagine, will crash pretty quickly.

You probably want to call std::vector::operator[] from a member variable. For example, if your class has a private vector object called vec, you might want to do:

double& Matrix::operator[](int i) 
    return this->vec[i];

Also, this->member is shorthand for (*this).member, use it more often!