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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:03:53+00:00 2026-05-31T02:03:53+00:00

I’ve got a design problem in a C++ project. I have a class, named

  • 0

I’ve got a “design” problem in a C++ project.

I have a class, named Currency (which can be "USD", "EUR", and so on…, and got some methods)
There are many instances of this class new-ed everywhere in the project, but there can only be a bunch of different currencies (~100).

So I wrote a method which allocates a Currency the first time it’s asked, and returns an existing Currency otherwise :

    class Currency 
    {
    public:
      typedef std::map<std::string, Currency*> CurrencyMap_t;
    public:
      static CurrencyMap_t _currencies;

    public:
      static const Currency& getCcy(const std::string& name)
      {
        CurrencyMap_t::const_iterator it(_currencies.find(name));

        if (it == _currencies.end())
          it = _currencies.insert(std::make_pair(name, new Currency(name))).first;

        return *(it->second);
      }

    private:
      // can't instantiate from outside
      Currency();
      Currency(const Currency& other);
    private:
      // private ctor
      explicit Currency(const std::string& name) {... }
    };

So, now, I only have one instance of each different Currency.

But, I can’t anymore have a class holding a Currency member, because the default-constructor is not defined :

    class CcyPair
    {
    public:
      CcyPair(const Currency& ccy1, const Currency& ccy2) {}
    private:
      Currency _ccy1; // won't compile because "no default-constructor available"
      Currency _ccy2;
    };

And I don’t want to hold Currency pointers in CcyPair class.

Do you have a better way to implement such a “pattern” which ensures that if two instances of a class (here the Currency class) got the same attributes, then it’s in fact the same instance (same underlying reference) ?

  • 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-31T02:03:54+00:00Added an answer on May 31, 2026 at 2:03 am

    I know you said you did not want to hold pointers to the Currency objects, but how would you feel about holding a reference instead? I’m not sure if you wanted to avoid pointers so you wouldn’t be burdened by a lot of checks against NULL or if you had another reason. Here is an example CcyPair using references:

      class CcyPair
          {
          public:
            CcyPair(const Currency& ccy1, const Currency& ccy2) 
              : _ccy1(ccy1), _ccy2(ccy2) {}
    
            CcyPair(const std::string& ccyName1, const std::string& ccyName2) 
              : _ccy1(Currency::getCcy(ccyName1)), _ccy2(Currency::getCcy(ccyName2)) {}
    
          private:
            // need to make these const since getCcy returns const 
            // you could also change the signature of getCcy to return non-const
            const Currency & _ccy1;
            const Currency & _ccy2;
          };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace

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.