3. Unary pre #
Created Saturday 11 January 2020
Important Questions #
- How is return working in the memory for a function?
- If we overload ++, is it prefix or postfix?
- Unary vs binary vs ternary in OOP?
Unary #
There are two possibilities Without Nesting
void operator++()
{
a++;
}
With Nesting
Number operator++()
{
a++;
return *this;
}
- This is very interesting. If we try ++(++obj), value gets increased by 1 and not 2. Why?
- System buffers are responsible for this
- When we return an object, only it’s value is returned. i.e Return by value is the norm. Hence any changes on the returned value will not cause a change in the original variable.This can be avoided by using pointers. But that’d be complex(when using the function). Instead, we set the return type as reference.
- This is required in order to facilitate **nesting of operators. **We need to hold the value, until we are done working on it.
- Solution - Return
Number&
, i.e no extra buffer is created, an alias will be returned. This means that we are working on the same object.