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

  • Home
  • SEARCH
  • 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 3485304
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:53:29+00:00 2026-05-18T10:53:29+00:00

In a new piece of code I have several different classes that refer to

  • 0

In a new piece of code I have several different classes that refer to each other. Something like this (this is not my actual situation but an example of something similar):

class BookManager
   {
   ...
   };

class Book
   {
   public:
      void setBookManager(BookManager *bookManager) {m_bookManager = bookManager;}
   private:
      BookManager *m_bookManager;
   };

Every book refers to a book manager, but the problem is that many books will have its own specific BookManager, but some books may share a common BookManager.

The caller doesn’t really specify what the Book should do with its BookManager, but in about 90% of the cases, the BookManager can be destroyed together with the Book. In about 10% of the cases, the same BookManager is reused for multiple books, and the BookManager must not be deleted with the Book.

Deleting the BookManager together with the Book is handy in those 90% of the cases, as the caller of Book::setBookManager doesn’t need to remember the BookManager anymore. It just dies with the Book itself.

I see two alternative solutions for solving this.

First is to make extensive use of shared pointers. If the caller is not interested anymore in the BookManager afterwards, it doesn’t keep a shared pointer to it. If it is still interested in it, or if it wants the BookManager to be shared over multiple books, it keeps the shared pointer and passes it to those multiple books.

A second alternative is to tell the Book explicitly what to do with the ownership of the book, like this:

class Book
   {
   public:
      void setBookManager(BookManager *bookManager, book takeOwnership=true)
         {
         m_bookManager = bookManager;
         m_hasOwnership = takeOwnership;
         }
      ~Book()
         {
         if (m_hasOwnership && m_bookManager) delete m_bookManager;
         }
   private:
      BookManager *m_bookManager;
      bool m_hasOwnership;
   };

The second solution seems much easier and allows us to use normal pointer syntax (BookManager * as opposed to std::shared_ptr<BookManager>), but it seems less ‘clean’ than the shared pointer approach.

Another alternative might be to have a typedef in BookManager like this:

class BookManager
   {
   public:
      typedef std::shared_ptr<BookManager> Ptr;
      ...
   };

Which allows us to write this:

BookManager::Ptr bookManager;

Which looks more like the the normal pointer-syntax than the original shared pointer syntax.

Does anyone have experience with either approach?
Any other suggestions?

  • 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-05-18T10:53:30+00:00Added an answer on May 18, 2026 at 10:53 am

    In C++ if you have shared, un-coordinated access to common objects then the most common approach is some kind of reference counting, which you get from shared_ptr.

    The only downside is that it isn’t pervasive in C++ (especially libraries), so you sometimes need access to the raw pointer. In those cases, you need to be careful to keep the object alive.

    I suggest that if you used shared_ptr — try to use it everywhere, unless it’s impossible. And yes, use the typedef if you want.

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

Sidebar

Related Questions

No related questions found

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.