C ++ ++ operator overload

which of the two codes is right and why?

C C::operator++() {
    x++; 
    y++; 
    return *this; 
}


C & C::operator++() {
    x++; 
    y++; 
    return *this; 
}

thanks

+4
source share
2 answers

The second is idiomatic: the-less option operator++is a pre-fix increment operator that should return a reference to self.

+7
source

Both are "correct", but the second is idiomatic because he expected the prefix to operator ++return an lvalue.

+4
source

Source: https://habr.com/ru/post/1526400/


All Articles