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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:25:30+00:00 2026-06-14T23:25:30+00:00

Ok, I have a singleton class GraphMaster which contains a number of system-wide values.

  • 0

Ok, I have a singleton class GraphMaster which contains a number of system-wide values. I have a subclass GraphObject : GraphMaster which has graph specific data. By subclassing, I can access members of either the global class or subclass. And by using a singleton class, I can change the global variables anywhere and have them be reflected in all the subclasses.

However, I’m getting stuck because the base class’s constructor wants to call the singleton class’s constructor, but it can’t as it’s marked private.

how do I get around this? Is what I’m trying to do possible? I went down this path due to responses to this post: Can I make a "global" object to store variables for multiple objects?

For example,

public class GraphMasterObject {
    private static GraphMasterObject instance;
    private GraphMasterObject() {   }
    }

    public static GraphMasterObject Instance {
        get {
            if (instance == null) instance = new GraphMasterObject();
            return instance;
        }
    }
    public int globalVar=10;
}

public class GraphObject : GraphMasterObject {

   public GraphObject() {
   }

   public int localVar=20;
}

I want to be able to do

GraphObject go = new GraphObject();
go.globalVar <- this is 10
GraphMasterObject.Instance.globalVar = 20;
go.globalVar <- now this is 20 
  • 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-14T23:25:32+00:00Added an answer on June 14, 2026 at 11:25 pm

    Ok, I have a singleton class GraphMaster which contains a number of system-wide values. I have a subclass GraphObject : GraphMaster which has graph specific data.

    That’s a problem to start with. As soon as you have a class which has subclasses, that it by definition not a singleton. Someone can add another subclass at any point, and even if you only have one instance of each subclass, you’ll have two distinct instances which are compatible with the base class.

    You could add something in the base class constructor to throw an exception if there’s already an instance, but it would be pretty smelly. Fundamentally, singletons are incompatible with subclassing. Rethink your design. (Ideally, avoid the singleton pattern in the first place, but that’s another matter…)

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

Sidebar

Related Questions

I have a singleton class, that has a map which can be accessed by
I have a Singleton Class SharedDataObject which has a another class object myClass. MyClass
I have a singleton class which is being used throughout the app. I am
Okay, newbie multi-threading question: I have a Singleton class. The class has a Static
I have a class (singleton) and it contains a static Dictionary private static Dictionary<string,
I have some class which uses boost singleton. It calls some function from own
I have a singleton parent class extended by a child class which is not
I have a java singleton class that has my application settings. I used this
I have a Singleton class which is highly dependent on the XML file it
I have a Singleton Class which loads some data on its construction. The problem

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.