mercredi 26 mai 2021

Why should I use *this operator?

Some lines of operator overloading inside a class definition look like:

class Field{
public:
   //constructor:
    Field(/*variables*/):/*code*/{
    //CODE
    }

//destructor:
~Field(){ 
//CODE
}

const int A, B, C;

double ***data;

Field& operator= (double s){
    for(int i = 0; i < A; i++)
        for(int j = 0; j < B; j++)
            for(int k = 0; k < C; k++)
                data[i][j][k] = s;
    return *this;
  }
}

What does the last line return *this make? As far as I understand, this is used to retrieve global objects with the same name of local, but this is not the case here, since a simple return s would give the same result, right?

Aucun commentaire:

Enregistrer un commentaire