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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:46:38+00:00 2026-06-10T16:46:38+00:00

I have a factory class that will be used to create a whole lot

  • 0

I have a factory class that will be used to create a whole lot of instances of a certain class. The creation process is pretty obfuscated and might require a quite a while. So I was thinking it would be smart to store the instances of the class which have already been created inside the factory, I can recall them at a later time.

The creation depends on a single parameter (the name) so things can be stored in a std::map which I call old_instances.

A A_factory::make_A(std::string const& name)
{
    if ( old_instances.find(name) != old_instances.end() )
    {
        return old_instances.find(name) -> second;
    }
    else
    {
        // obfuscated creation process that creates instance 'new_A'
        // ...

        old_instances.insert(std::pair<std::string, A>(name, new_A)); // <- problem
        return new_A;
    }
}

The problem here is that this whole routine could be a const member of the factory. But because the old_instances is adapted.

I kinda feel awkward sacrificing the const-ness of a function for something this trivial.
Are these kind of sacrifices logical?

please do not shoot me if this question is too dependent on taste.

  • 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-10T16:46:39+00:00Added an answer on June 10, 2026 at 4:46 pm

    This is the classic example of using mutable. You should be fine doing this as long as the external behavior truly is that of const.

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

Sidebar

Related Questions

In one app, I have a task to create files that will be used
I have a class representing a set of values that will be used as
I have a factory class, DocumentLoaderFactory , which simply returns an instance that implements
I have a class that is used as a member in many places in
I have a generic abstract class Factory<T> with a method createBoxedInstance() which returns instances
I have a Generic Class Factory class that has two methods one utilizes the
I have a simple class ExcelStringBuilder. Its used to build a string that can
I have the following scenario: I have a QueueReader class that will be reading
So I have a factory class and I'm trying to work out what the
I have the following factory class: public class MessagePresenterCreator implements IPresenterFactory{ @Override public MessagePresenter

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.