Object object = *(queue.dequeue());
queue is a QQueue<Object*>. i’m concerned that the dequeued pointer is deleted before i dereference it. of course i can access object‘s data but that doesn’t mean anything. doing it all in one line like this is convenient (i don’t want a pointer because i want object to be deleted automatically when it goes out of scope) but if it’s not safe then i can’t do it. please advise. thanks.
It isn’t immediately unsafe per se, but chances are you’ll be using it wrong.
All your code does is make a copy of the element pointed to by the last element of
queue. By dequeuing the element, you lose the pointer to it, and you now have no way of freeing that original object in case it was dynamically constructed.Correct example (no dynamic objects, no leak):
Broken example (dynamic object leaked):
You’re right that
cis safely destroyed at the end of its scope, but you’re leaking the orginal, new’ed object, to which you have no pointer or reference now.