It is worth remembering that the & () operator can be overloaded for a variable type, have some side effects and optimize this instruction by changing the behavior of the program.
One example is the smart pointer used to manage objects other than C ++, _ com_ptr_t . It has an overloaded _com_ptr_t :: operator & () that checks if the pointer inside already contains some non-zero address. If it turns out that the stored address is not equal to zero, this means that the pointer is already bound to some object. If this happens, the operator _com_ptr_t :: operator & () disables the object - calls IUnknown :: Release () and sets the pointer to zero.
A side effect is needed here because this is a typical use:
_com_ptr_t<Interface> pointer;
CoCreateInstance () or other object search code has no idea about C ++ and _com_ptr_t, so it just rewrites the address passed into it. Therefore, _com_ptr_t :: operator & () must first free the object to which the pointer is bound, if any.
So for _com_ptr_t this statement is:
&variable;
will have the same effect as
variable = 0;
and its optimization will change the behavior of the program.
source share