References are not objects. They don't necessarily require any allocation of memory. They therefore don't have a lifetime for you to worry about.
The scope of the reference is your Draw
function, which means that you won't be able to access the reference from outside of the function, but that's besides the point.
The only concern you need to have is about the lifetime of the object that your reference refers to. It's certainly possible for a reference to refer to an invalid object, because the lifetime of the object it was referring to has ended.
Pointers, however, are objects. If you had used a pointer instead, the pointer would have automatic storage duration because it is just a simple variable declaration. This means the pointer object's lifetime would last until the end of the function (the same as its scope). So even though pointers are objects, there's no extra concern (although pointers in general are usually not the best idea).
The allocation and deallocation of objects only becomes complex when you start dealing with new
and delete
, which create objects with dynamic storage duration. You should try to avoid this as much as possible. Note that in int* p = new int(5);
, it is not the pointer that you need to worry about, but the int
that the pointer points at. Note that it's impossible to dynamically allocate a reference (as it might have nothing to allocate!).