Is there a way to "convert" a reference to pointer in c++? In example below, func2
has already defined prototype and I can't change it, but func
is my API, and I'd like to either pass both parameters, or one (and second set to NULL) or neither (both set to NULL):
void func2(some1 *p1, some2 *p2);
func(some1& obj, some2& obj2)
{
func2(..);
}
func2(&obj, &obj2)
– Szombathelyfunc2(&obj1, NULL)
. Note that there is no way to passNULL
tofunc
; there is no such thing as aNULL
reference. – Hospodarint* pi = 0; int& ri = π
. Of course that's naughty, but not impossible and sometimes done by accident. – HalimedaNULL
reference. It is an abomination. It is Undefined. It is likely to invoke Nasal Demons. It does not exist. – Hospodar&ri
, and so comparing it againstnullptr
orNULL
is fruitless. It may have appeared to work as you expected in particular situations, but there is no guarantee of it working in other implementations, or in the same implementation at different times. Undefined behavior sometimes means behaving exactly as you expect. – Butterball