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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:47:38+00:00 2026-05-26T23:47:38+00:00

This post is what I just read. The way he implements Singleton in C++

  • 0

This post is what I just read.

The way he implements Singleton in C++ confuses me. I got several Questions about it and here is his code:

template<typename T>
class Singleton {
public:
    static T& getInstance() {  //Question 1
        return instance;
    }
private:
    static T instance;
};

class DebugLog : public Singleton<DebugLog> {  //Question 2
public:
    void doNothing() {}
};

Question

  1. I think we should put the static T& getInstance()‘s definition outside of the class body, right?

  2. He tries to make class DebugLog a singleton class, but when he inherits Singleton<DebugLog>, the DebugLog doesn’t already exist, right? If right, then how can the template class Singleton instantiate an un-existent class?

  • 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-26T23:47:39+00:00Added an answer on May 26, 2026 at 11:47 pm
    1. It would certainly be cleaner if the function were defined outside of
      the class, and make the code easier to read and to maintain. In this
      case, however, the complete class is small enough that the difference
      isn’t very great, and of course, because we’re dealing with a template,
      the actual definition still has to be included in every translation unit
      which uses it.

    The C++ standard doesn’t speak of “existence” with regards to classes (or anything
    else). At the point of template instantiation, name lookup finds
    DebugLog, and finds that it is a class (thus, a type). At that point,
    it is an incomplete type, and there are only limited things you can do
    with an incomplete type. If the class template which is instantiated
    doesn’t do anything that requires a complete type (and Singleton
    doesn’t), then there is no problem. (Note that only the class
    definition is instantiated at this point; class member functions will
    not be instantiated until they are used.)

    I might add that there is still one important thing missing from the
    code you posted: there is no definition for the declaration
    Singleton::instance. You still need to add a:

    template<typename T> T Singleton<T>::instance;
    

    somewhere.

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

Sidebar

Related Questions

I just read this post about why new-line warnings exist, but to be honest
Just finished read this post by Greg Young, where he is talking about Microsoft
I just read this post about adding buttons with a loop programmatically . I
So I saw this post here and read it and it seems like bulk
I just read this post and it makes the case against implicit typing using
Just read this interesting article by Omar on his blog Linq to SQL solve
I saw this post: Typos… Just use option strict and explicit please.. during one
I just finished reading this post: https://developer.yahoo.com/performance/rules.html#flush and have already implemented a flush after
This is going to be a long post and just for fun, so if
Just finished reading this blog post: http://www.skorks.com/2010/03/an-interview-question-that-prints-out-its-own-source-code-in-ruby/ In it, the author argues the case

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.