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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:48:58+00:00 2026-05-20T01:48:58+00:00

A question about c++ design and efficiency… Imagine this code – Database db; class

  • 0

A question about c++ design and efficiency… Imagine this code –

Database db;

class SmallObject
{
private:
    /* Some small amount of data and functions */
public:
    /* Constructor, more functions */
    void fn1() { /* Code that uses global db to do it's job */ }
    void fn2() { /* Code that uses global db to do it's job */ }
    void fn3() { /* Code that uses global db to do it's job */ }
    void fn4() { /* Code that uses global db to do it's job */ }
};

That’s nice, efficient and works well. However the database is global state which is bad. It makes it very hard to test the class in isolation etc and means that SmallObject depends on a specific implementation of Database.

So I restructure it this way :-

class SmallObject
{
private:
    /* Some small amount of data and functions */
public:
    SmallObject(Database& db) { db_ = db; }

    void fn1() { /* Code that uses global db_ to do it's job */ }
    void fn2() { /* Code that uses global db_ to do it's job */ }
    void fn3() { /* Code that uses global db_ to do it's job */ }
    void fn4() { /* Code that uses global db_ to do it's job */ }
};

And perhaps make a factory to create them so I can use them roughly as before.

class SmallObjectFactory
{
private:
    Database* db_;
public:
    SmallObjectFactory()
    {
        db_ = new SpecificDatabaseSubclass;
    }

    SmallObject* create() { return new SmallObject(db_); }
};

That works too, means that my SmallObject is no longer coupled to the exact database implementatiom but is still convenient to use.

However, imagine there are 10000 instances of SmallObject. Now each one has a pointer to the same Database object in there. That’s horribly inefficient, and also I don’t feel that duplicating the data so many times is good design…

So, is there some pattern in C++ that allows me to not make Database global and yet allows me not to store duplicate data in each instance of SmallObject?

This is specifically C++ so templates etc, are fine…

  • 1 1 Answer
  • 1 View
  • 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-20T01:48:59+00:00Added an answer on May 20, 2026 at 1:48 am

    If you are looking for efficiency it may well be the way you are allocating large numbers of small objects and might want to use some kind of pool.

    With regards to the database, I would question your coupling and what users of your classes are doing and seeing. Ideally you do not want them to see your implementation detail, which may be an issue if your class has this private “database” member in it although you can forwardly-declare it.

    Your constructor should look like:

       explicit SmallObject(Database& db) : db_(db) {}
    

    You should also handle SmallObjectFactory properly to manage the lifetime of the underlying db member, probably making your class non-copyable or using shared_ptr.

    Your other option might be to maintain a collection of small objects and perform functionality that accesses the database through the container rather than the objects. This is a common design pattern but depends on your model.

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

Sidebar

Related Questions

This is a general question about mongodb database design, but the reason for my
A niave beginners question about database design. I have an app managing some logger
I have posted a question about multilanguage database design here, [] What are best
This is a question about generic c++ event driven applications design. Lets assume that
This is probably a very basic question about AD FS 2.0 claim rule design
This is a more a philosophical question about Apple's design decisions than a question
This is a question about Application design rather than how-to-do specific actions. I am
I have a question about Database design. I have two tables, usertable and historytable,
This is a question about Qt library, not about Web design. For QLabel and
This is a question about design approach. I have limited COM experience and a

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.