#includeIn this example, we create an instance of MyClass and pass a SafePtr to it to the foo function. When foo tries to dereference the SafePtr, it first checks whether the referenced object is still valid. If not, the dereferencing is skipped, and no crash occurs. The HostileReference library is open source and can be found on GitHub.class MyClass { public: void doSomething() { } }; void foo(const HostileReference::SafePtr & ptr) { if (ptr) { ptr->doSomething(); } } int main() { MyClass* myObj = new MyClass(); foo(HostileReference::SafePtr (myObj)); delete myObj; // The following line would cause a crash due to a dangling reference // foo(myObj); }