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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:17:20+00:00 2026-06-12T03:17:20+00:00

class A { private: A(){}; ~A(){}; public: static A* GetInstance( void ) { static

  • 0
class A
{
private:
    A(){}; 
    ~A(){};
public:
    static A* GetInstance( void )
    {
        static A obj;
        return& obj;
    }
};

I was supposing if something bad happens would only happen at the first time, since the class constructor is only initialized the first time a call to GetInstance happens, I don’t know the great depths of C++ and I do not trust C++, so when I need a pointer to this class that will be used many times in a function I’m currently doing:

A* ptr = A::GetInstance();
if( ptr )
{

Checking for the pointer validity, I believe that the method returns the address where the value of obj is stored so that ptr will point to it, which I guess it can't fail.

Please observe that I’m not talking about a small application, I’m currently developing on a 500,000+ lines MMO server application that handles thousand of clients and has to stay opened for weeks without crashing, defensive programming is the least minimum required. Is it for sure, 100% safe to use ptr without checking its validity?

  • 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-12T03:17:22+00:00Added an answer on June 12, 2026 at 3:17 am

    There is certainly no way that that function will ever return a null pointer, so there’s no point checking for that. It might make more sense to return a reference, to make it clear that it won’t be null.

    However, there’s no guarantee that the function will always return a pointer to a valid object. If it’s called from the destructor of a static object, then obj might already have been destroyed. This is one reason why globally accessible objects are a very bad idea, whether or not you wrap them up in the Singleton anti-pattern like this.

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

Sidebar

Related Questions

public static class TestTask extends AsyncTask<Void, Integer, Integer> { private String stacktrace; public TestTask
public class SelfCallingTest { private static int counter; public void SelfCallingMethod(int counter) { Console.WriteLine(The
public interface DownloadListener { public void onDownloaded(); } public class DownloadManager { private static
public class Readparam { private static String method_name; public static HashMap<String, Vector<String>> getParameters(String file_name)
The class looks as follows: public static class CacheManager { private static Dictionary<string, object>
I have this class: public static class CsvWriter { private static StreamWriter _writer =
public class A { private A(int param1, String param2) {} public static A createFromCursor(Cursor
Let's say I have a class called AppConfig: public static class AppConfig { private
public class ExampleActivity extends Activity { public static final int DIALOG_DOWNLOAD_PROGRESS = 0; private
public class Checker { static private int value1 = 0, value2 = 1; static

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.