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!

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.'

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

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

class Handle {
    Handle(const Base &b):p(b.clone()), use(new std::size_t(1)) { }
    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?



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

Maximum Likelihood Estimation of a Poisson Distribution?

java algorithm math statistics multiplatform

I have a table with observations (x, y) and need to estimate the mean of the Poisson distribution that more closely resembles them. It seems R and Octave can both do this on Linux, but I was wonder...

What kind of timestamp is in the form '07:df:01:0e:0e:28:2a:39'?

java date format

I am trying to receive trap from UCS and convert them into JSON format.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.