42-CPP-GIT / CPP_GIT

Git Code Review로 CPP Module를 때려잡자 ☄️
9 stars 1 forks source link

전위 증감 연산자에서 리턴을 레퍼런스로 하는 이유? #40

Open aLVINlEE9 opened 1 year ago

aLVINlEE9 commented 1 year ago

https://en.cppreference.com/w/cpp/language/operators

Although the canonical implementations of the prefix increment and decrement operators return by reference, as with any operator overload, the return type is user-defined; for example the overloads of these operators for [std::atomic]

canonical 구현은 return by reference인데, 사실상 user가 마음대로 할 수 있습니다. return by value로도 구현해도됩니다.

change-challenge commented 1 year ago
    Fixed       &operator++(void);
    Fixed       &operator--(void);

@aLVINlEE9 승슬님 지금 이 부분 말씀하시는거죠? 저는 내부에

Fixed       &Fixed::operator++(void)
{
    this->_fixedNum++;
    return *this;
}

Fixed       &Fixed::operator--(void)
{
    this->_fixedNum--;
    return *this;
}

이런 식으로 정의를 내렸는데 이게 *this를 리턴하기때문에 레퍼런스가 아니여도 된다는 건가요?

aLVINlEE9 commented 1 year ago

넵넵