#include <RRefable.hh>
You can then give an instance of your class (or subclass thereof) to an RRef. When the Foo goes out of scope and gets destroyed, any RRefs referencing that Foo will know that it no longer exists and an assertion will fail if you attempt to access that Foo through the RRef.
In a release build (i.e. when -DNDEBUG is given to compiler), RRefable is completely empty so there is no cost for a release build, but you get reference checking in debug mode.
RRefable does not include any data and so does not need a virtual destructor. Since RRefable has no public interface, it wouldn't make much sense to use it polymorphically anyways.
class Foo: public RRefable { public: virtual void method() {...} }; class DFoo: public Foo {...}; int main() { RRef<Foo> foo; Foo* aFoo = new DFoo; foo = *aFoo; // create RRef to aFoo assert(foo.isNotNull()); // succeeds delete aFoo; assert(foo.isNull()); // succeeds foo().method(); // causes assertion to fail }
Definition at line 29 of file RRefable.hh.
Protected Member Functions | |
RRefable (const RRefable &) | |
A copy has its own ref usage, so nothing to do (but default one by compiler would be wrong). | |
void | operator= (const RRefable &rhs) |
A copy has its own ref usage, so nothing to do (but default one by compiler would be wrong). |