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

  • Home
  • SEARCH
  • 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 6038667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:15:20+00:00 2026-05-23T06:15:20+00:00

I have a Singleton class here designed to be inherited by classes wanting to

  • 0

I have a Singleton class here designed to be inherited by classes wanting to use it. But I have a question about something the compiler is allowing and I don’t understand why:

template<class DerivedT>
class Singleton {
  static DerivedT*& internalPointer() {
    static DerivedT* object(nullptr);
    return object;
  }

  void reassignIf(Singleton* instance) {
    auto& object(internalPointer());
    if (object == static_cast<DerivedT*>(instance)) {
      object = static_cast<DerivedT*>(this);
    }
  }

  Singleton(Singleton const& other); // = delete

  Singleton& operator=(Singleton const& other); // = delete

public:
  static DerivedT* instance() { return internalPointer(); }

protected:
  Singleton() { reassignIf(nullptr); }

  Singleton(Singleton&& other) { reassignIf(&other); }

  ~Singleton() {
    auto& object(internalPointer());
    if (object == static_cast<DerivedT*>(this)) {
      object = nullptr;
    }
  }

  Singleton& operator=(Singleton&& other) { reassignIf(&other); return *this; }
};

class SingletonMock: Singleton<SingletonMock> {
public:
  using Singleton<SingletonMock>::instance;

  SingletonMock() : Singleton<SingletonMock>() { }
};

int main() {
  SingletonMock x;
  SingletonMock const y;  // why is this line allowed?
}

Why is the bottom line allowed? The Singleton base class has a static member function called internalPointer that has a static DerivedT*. This should become SingletonMock*, but it’s allowing a const SingletonMock to be assigned to that variable.

EDIT:

I think I understand why it’s working now from the comments. However, specifically it’s allowing me to do this:

SingletonMock const y;
SingletonMock* yPtr(SingletonMock::instance());

How can I stop something like that from being allowed?

  • 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-23T06:15:20+00:00Added an answer on May 23, 2026 at 6:15 am

    I think the question is: Why does the compiler allow me to declare a const Singleton? I’m assuming you are wondering how the compiler can let you create a const Singleton when you only want to have pointers to a non-const Singleton. If you declare a const instance of a class that has a constructor, it still needs to be constructed. In that constructor, it is not const. Once the constructor finishes, it can only be accessed as const, but that doesn’t apply while it’s being constructed. Otherwise, you couldn’t have const instances of any class.

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

Sidebar

Related Questions

I have a C# singleton class that multiple classes use. Is access through Instance
Okay, newbie multi-threading question: I have a Singleton class. The class has a Static
I have a a singleton class here is the code #import <Foundation/Foundation.h> #import CustomColor.h
I have some singleton class (please, don't speak about singleton usage). class InputSystem :
I feel like I'm missing something here, but I have this datagrid which when
So I have the following: public class Singleton { private Singleton(){} public static readonly
I have a co-worker that swears by //in a singleton Constants class public static
I have a singleton class for global access to config information. This singleton class
I have a webapplication where I use a registry class. The registry class holds
I have some class it is a singleton we have this class in already

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.