When is a pointer needed in a return of an operator function?

1

I am studying for a term and I want to know when we use the return of the operator functions.

For example, why is the pointer needed in return !((*this)==v); ?

class vecteur3d
{    float x;
     public :
     vecteur3d(floatx=0.0)
     {
        x=c1;
     }
     int operator!=(vecteur3d);
};

int vecteur3d::operator !=(vecteur3d v)
{
    return !((*this)==v);
}
    
asked by ThePassenger 10.05.2017 в 14:27
source

2 answers

3
int vecteur3d::operator !=(vecteur3d v)
{
    return !((*this)==v);
}

The function itself tells you that it will return an integer, then no, the function will not return a pointer but simply an integer. What we should ask ourselves here is what is the sense of this operator returning an integer instead of a Boolean, which would be natural.

What does that return do?

return !((*this)==v);
//        ^^^^^

The asterisk operator * is used in this case to dereference the pointer, that is, it is a way to access the content managed by the pointer:

int* ptr = new int(10);
int var = *ptr; // var = 10
std::cout << var;

We continue ...

return !((*this)==v);
//              ^^

We have already seen that with the operator * we are accessing the pointed object, which in this case is type vecteur3d . The comparison operator is comparing this object with v , which is also type vecteur3d . The result of the comparison will be a Boolean.

return !((*this)==v);
//     ^

This last operator will invert the value returned by the previous comparison. If it turns out that *this equals v the comparison operator will return true and this operator will convert it to false and vice versa.

If we break the line of code, the sequence of instructions is better:

int vecteur3d::operator !=(vecteur3d v)
{
  vecteur3d yo = *this;
  bool iguales = (yo==v);
  return !iguales;
}

In summary. This is a simple way to implement the operator "other than" without having to repeat the logic implemented in the comparison operator.

    
answered by 10.05.2017 / 14:52
source
3
  

For example, why is the pointer needed in return !((*this)==v); ?

Eye, you are not returning the pointer, you are de-referencing it. Preputing the asterisk ( * ) to a pointer type data returns a reference to the pointed data, so we could break down the entire instruction as follows:

int vecteur3d::operator !=(vecteur3d v)
{
    vecteur3d &referencia = *this;
    bool comparacion = (referencia == v);

    return !comparacion;
}

As you see, it is not strictly necessary to do everything in one line, but it is quite clear when you know what it means. In this case, you are de-referencing the pointer this to access the operator == (which I do not see declared in class vecteur3d ), another way to write this instruction would be:

int vecteur3d::operator !=(vecteur3d v)
{
    return !this->operator ==(v);
}

It is common practice to write operators based on the opposite operator (for example < based on > , == based on != ) to avoid duplicating code and facilitate maintenance.

    
answered by 10.05.2017 в 14:31