Handle class in c++?

In c++ primer 15.8, when author talked about 'handle class and inheritance', he said:

'A common technique in C++ is to define a so-called cover or handle class . The handle class stores and manages a pointer to the base class. The type of the object to which that pointer points will vary; it can point at either a base- or a derived-type object. Users access the operations of the inheritance hierarchy through the handle. Because the handle uses its pointer to execute those operations, the behavior of virtual members will vary at run time depending on the kind of object to which the handle is actually bound. Users of the handle thus obtain dynamic behavior but do not themselves have to worry about managing the pointer.

The above sounds very much like a smart pointer to me. However, the usage of this 'handle' is a little different.

Handle h(Derived()); // noticed a derived object, not a pointer is used to initialize the handle!
h->func();

And a latter implementation of the handle (shown below) proves what the author comments on the handle: 'We'd like users of our handle to create their own objects, to which they could attach a handle. The handle will allocate a new object of the appropriate type and copy user's object into that newly allocated object. That way the handle class will own the object and can guarantee that the object is not deleted until the last handle attached to the object goes away.'

public:
    Base(){}
    virtual int func(std::size_t n) const {}
    virtual Base* clone() const { return new Base(*this); }
};

class Derived : public Base {
public:
    Derived():Base(){}
    int func(std::size_t) const; // redefine
    Derived* clone() const { return new Derived(*this); }
};

class Handle {
public:
    Handle(const Base &b):p(b.clone()), use(new std::size_t(1)) { }
private:
    Base *p;
    std::size_t *use; // pointer to shared use count
};

My question is: is this really the typical meaning of handle in c++ or OO? I thought that handle can be just a base type smart pointer that keeps use-count internally, or more generally some magic int type that system knows what it points to and what to do with it. Why would this handle here need to make a copy of the user object to itself and then do use-count based on the copy?

Anyone helps?

Thanks,

Answers


The idiomatic equivalent in modern C++ would be:

std::shared_ptr<Base> h{std::make_shared<Derived>()};

In general, a "handle" implies some form of type erasure, so that code dealing with the handle doesn't need to know the details of how the functionality is implemented.

The reason the code you've shown is using clone to copy the object is precisely in order to perform type erasure; at the point the clone method is called the knowledge of the actual type of the Derived object is erased. In terms of implementation quality, the code is non-thread-safe, which is irrelevant for legacy C++ but an issue for modern C++, which does have a thread-aware memory model. It is also not obviously exception-safe; it is much better to delegate exception safety to standard library classes like shared_ptr. Finally, the Base class is lacking a virtual destructor.


Need Your Help

Getting started with SOAP for Delphi in Win32

delphi soap remobjects

I have a server process built in Delphi/C++Builder with RemObjects SDK which claims to support SOAP requests.

How to group latitude/longitude points that are 'close' to each other?

sql database geolocation location cluster-analysis

I have a database of user submitted latitude/longitude points and am trying to group 'close' points together. 'Close' is relative, but for now it seems to ~500 feet.