The following code example is taken from the book
C++ Templates - The Complete Guide
by David Vandevoorde and Nicolai M. Josuttis, Addison-Wesley, 2002
© Copyright David Vandevoorde and Nicolai M. Josuttis 2002
#ifndef TRULE_HPP
#define TRULE_HPP
template <typename T>
class Holder;
template <typename T>
class Trule {
private:
T* ptr; // objects to which the trule refers (if any)
public:
// constructor to ensure that a trule is used only as a return type
// to transfer holders from callee to caller!
Trule (Holder<T>& h) {
ptr = h.get();
h.release();
}
// copy constructor
Trule (Trule<T> const& t) {
ptr = t.ptr;
const_cast<Trule<T>&>(t).ptr = 0;
}
// destructor
~Trule() {
delete ptr;
}
private:
Trule(Trule<T>&); // discourage use of lvalue trules
Trule<T>& operator= (Trule<T>&); // discourage copy assignment
friend class Holder<T>;
};
#endif // TRULE_HPP