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 7093941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:30:07+00:00 2026-05-28T08:30:07+00:00

I have a singleton with a following structure: // Hpp class Root : public

  • 0

I have a singleton with a following structure:

// Hpp
class Root : public boost::noncopyable
{
    public:
        ~Root();

        static Root &Get();

        void Initialize();
        void Deinitialize();

    private:
        Root();  // Private for singleton purposes
        static boost::scoped_ptr<Root> mInstance;

        Manager1 *mManager1;
        Manager2 *mManager2;
};


// Cpp
boost::scoped_ptr<Root> Root::mInstance;

Root::Root()
{
    // [!!!]
    mInstance = this;

    // Managers are using `mInstance` in their constructors
    mManager1 = new Manager1();
    mManager2 = new Manager2();

    mInstance->Initialize();
}

Root::~Root() { delete mManager1; delete mManager2; }

Root &Root::Get()
{
    if (mInstance == nullptr) mInstance = boost::scoped_ptr<Root>(new Root());
    return *mInstance;
}

The idea is to remove instance automatically when program exists. Look at line marked [!!!] : mInstance = this. I have to do this because mManager1 and mManager2 are using mInstance in their purposes in ctor.

The problem is how to use this with boost::scoped_ptr? In shared_ptr case there is enable_shared_from_this class from boost which allows to get what I need. But what to do in my case?

  • 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-28T08:30:08+00:00Added an answer on May 28, 2026 at 8:30 am

    You can simply have the constructors take the Root as argument, and then pass this to them:

    Root::Root() {
        mManager1 = new Manager1(this);
        mManager2 = new Manager2(this);    
        Initialize();
    }
    

    One additional advantage of this is that you don’t get the other classes coupled to the singleton secretly.

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

Sidebar

Related Questions

So I have the following: public class Singleton { private Singleton(){} public static readonly
I have used singleton calss following the example: singleton class But i get the
I have the following Singleton Class in PHP. class CounterBalance{ private static $instance; private
I have a class with the following structure: class Something { private static $_instance
I have the following singleton class which contains the following property: public class Manager
I have the following code public class Something { [Inject] public Configuration config {get;set;}
I have a Singleton that is accessed in my class via a static property
Okay, newbie multi-threading question: I have a Singleton class. The class has a Static
I have created the following Singleton in my iPhone app: #import AppManager.h static AppManager
I have the following classes defined: public interface Thingy { ... } public class

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.