I\'m having a devil of a time understanding references. Consider the following code:
class Animal
{
public:
virtual void makeSound() {cout << \"rawr\"
To answer the second part of your question ("how do I communicate that the pointer is subject to deletion at any time") -
This is a dangerous practice, and has subtle details you will need to consider. It is racy in nature.
If the pointer can be deleted at any point in time, it is never safe to use it from another context, because even if you check "are you still valid?" every time, it may be deleted just a tiny bit after the check, but before you get to use it.
A safe way to do these things is the "weak pointer" concept - have the object be stored as a shared pointer (one level of indirection, can be released at any time), and have the returned value be a weak pointer - something that you must query before you can use, and must release after you've used it. This way as long the object is still valid, you can use it.
Pseudo code (based on invented weak and shared pointers, I'm not using Boost...) -
weak< Animal > animalWeak = getAnimalThatMayDisappear();
// ...
{
shared< Animal > animal = animalWeak.getShared();
if ( animal )
{
// 'animal' is still valid, use it.
// ...
}
else
{
// 'animal' is not valid, can't use it. It points to NULL.
// Now what?
}
}
// And at this point the shared pointer of 'animal' is implicitly released.
But this is complex and error prone, and would likely make your life harder. I'd recommend going for simpler designs if possible.