---
# System prepended metadata

title: Prefix vs Postfix increment

---

# C++ Prefix vs Postfix increment
## Prefix vs Postfix operator
### Prefix (++a): ```Fixed& operator++()```
- Return: reference to this
- Why &: To allow chaining, avoid copy
- Implementation
    ```c++
    Fixed& Fixed::operator++()
    {
        this->_value++;
        
        // return current obj by reference
        return (*this);
    }
    ```
    - Returns a reference to the same object after incrementing it
    - No copying needed - you get the actual incremented object
    - More efficient since no temporary object is created
    - The ```&``` indicates it returns a reference to ```*this```
### Postfix (a++): ```Fixed operator++(int)```
- Return: the original value before incrementing
- Why no &: Need to return previous value
- Implementation
    ```c++
    Fixed Fixed::operator++(int)
    {
        Fixed temp(*this); // save old value
        ++(*this); // use prefix to increment
        return (temp); // return old value
    }
    ```
    - Must return the original value before incrementing
    - Requires creating a copy of the original value to return
    - The object itself gets incremented, but the returned value is the old state
    - No ```&``` because it returns a temporary copy, not a reference