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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:05:38+00:00 2026-05-23T18:05:38+00:00

I use singletons a lot because I hate to pass an object of the

  • 0

I use singletons a lot because I hate to pass an object of the main class to member classes in order to allow them to access the main class.

Class Foo
{
    A* a;
    B* b;
    C* c;
};

For example in the above example if A,B and C would like to access Foo- I would have to pass the object of Foo each one of them, and store it as their member variable (or give the object in every function call). This works, but it does not feel right and requires more code writing.

Instead, I can make Foo a singleton (of course only if there can be only 1 instance) and then just call Foo::getInstance()->… from A, B and C. And I do not have to pass any objects. I find this very handy.

Now, the problem is that I have a situation where there can be multiple instances of Foo. Obviously I cannot use singleton pattern. But I do not want to pass variables nor store them in member classes. Too much code! 🙂

As an example,
I hate this:

A::A(Foo* foo) : m_foo(foo)
{

}

and this:

void A::someFunc(Foo* foo, int someParam)
{

}

But, I love this:

A::A()
{

}

void A::someFunc(int someParam)
{
    Foo* foo = Foo::getInstance();
}

Is there any other way to do it? Something that resembles the singleton pattern?

  • 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-23T18:05:39+00:00Added an answer on May 23, 2026 at 6:05 pm

    This kind of pattern is creating a bunch of circular references, which in general is a code smell. You may want to take a solid look at your design, because the usual way to solve these sort of relationship problems is to create a third class that interacts with both existing classes. Alternately, what wrong with passing along a reference to the containing class if you really need that behavior?

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

Sidebar

Related Questions

I use a lot of singletons, I have set up many as I call
I noticed in a custom base class that we use for a lot of
I am going to use singleton classes to manage both DB connections and references
I have a singleton object that use another object (not singleton), to require some
The setup: I have a singleton data access manager object, call it Manager. I
I use a unit of work pattern a lot in my flex projects. I'll
I have some c3p0 pool encapsulated in a class that I use to execute
I'm still learning how to use classes and I was wondering when should I
I've been noticing static classes getting a lot of bad rep on SO in
I'm using boost singletons (from serialization). For example, there are some classes which inherit

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.