DoubleOseven DoubleOseven - 1 year ago 150
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];

Answer Source

(*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!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download