Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8498501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:25:13+00:00 2026-06-11T00:25:13+00:00

In C++ primer 15.8, when the author talked about ‘handle class and inheritance’, he

  • 0

In C++ primer 15.8, when the 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.

Code:

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T00:25:15+00:00Added an answer on June 11, 2026 at 12:25 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

According to the C++ Primer book, the author mentioned that we can specify a
In c++ primer, pg 95 the author says that c++ programmers tend to use
Basic primer: class User has_many :programs, :through => :memberships has_many :memberships end class Program
I'm learning c++ and using C++ Primer. Consider the following exercise 14.46: class Complex
I am reading the C++ Primer, in the overloaded operation chapter, the author gave
C++ Primer says: The identifier we define in our programs may not contain 2
Possible Duplicate: Good Primer for Python Slice Notation reverse a string in Python I've
I am reading Chapter 17 of C Primer Plus, and here is the code
Can somebody point me to a good primer on the above, and what happens
I've started to learn C++ using C++ Primer by Stephen Prate and I'm currently

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.