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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:58:57+00:00 2026-05-31T13:58:57+00:00

Imagine I have a Device class. I don’t want the programmer to be able

  • 0

Imagine I have a Device class. I don’t want the programmer to be able to instantiate his own Device objects so now I create a DeviceManager class. The DeviceManager is the only entity that will be instantiated and gives the programmer access to Device objects by having the programmer specify an ID of some sort. This way, only the DeviceManager class needs to be cleaned up by the programmer and no stray Device objects are left behind. My main concern is how to control access to objects in the interest of studious garbage collection. Is a “manager” pattern like I’ve described a good idea in C++? Thanks!

  • 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-31T13:58:59+00:00Added an answer on May 31, 2026 at 1:58 pm

    If you really need it, yes.

    To achieve this control, you can make the constructor of the Device private and declare DeviceManager as protected in the Device class.

    class Device
    {
    private: //not necessary, private by default
       Device();
    
    friend class DeviceManager;
    };
    
    class DeviceManager
    {
    public:
       Device* getDevice();
    };
    

    You could also return a smart pointer to a Device (recommended) and keep created devices inside the DeviceManager, depending on what you need.

    This is called the instance manager pattern. The DeviceManager is also a factory of devices.

    Also, C++ doesn’t have garbage collection, but if you use a smart pointer you needn’t worry about memory management.

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

Sidebar

Related Questions

Imagine I have String in C#: I Don’t see ya.. I want to remove
Imagine I have the folling XML file: <a>before<b>middle</b>after</a> I want to convert it into
Imagine I have table like this: id:Product:shop_id 1:Basketball:41 2:Football:41 3:Rocket:45 4:Car:86 5:Plane:86 Now, this
I want to create an application that logs when I have gone jogging and
Imagine a full Android device screen, I want it split in to two sections:
Imagine you have a list of items which is an array of objects (fields:
Imagine I have a list of objects that implement an interface called ISummary The
Imagine I have a class in C# called Bar that has a public function
Imagine i have an extension method that operates of an interface of abstract objects:
Imagine we have a program trying to write to a particular file, but failing.

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.