3. Unary pre #

Created Saturday 11 January 2020

Important Questions #

Unary #

There are two possibilities Without Nesting

void operator++()
{
  a++;
}

With Nesting

Number operator++()
{
	a++;
	return *this;
}
  1. This is very interesting. If we try ++(++obj), value gets increased by 1 and not 2. Why?
  2. 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.