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

The Archive Base Latest Questions

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

Consider the following code parts: this is Timing.h: class Timing { public: //creates a

  • 0

Consider the following code parts:

this is Timing.h:

class Timing {

public:
    //creates a single instance of timing. A sinlgeton design pattern
    Timing *CreateInstance();
private:
    /** private constructor and a static instance of Timing for a singleton pattern. */
    Timing();
    static Timing *_singleInstance;
};

extern Timing timing;

and this is Timing.cpp:

Timing timing;  //a global variable of Timing

Timing *Timing::CreateInstance() {
    if (!_singleInstance) {
        _singleInstance = new Timing();
    }
    return _singleInstance;
}

Now, since I want to hold one object of Timing i made as a singleton pattern (only one timing can be created). In my exercise requirements, they say I can choose between 2 option:
create one instance of timing in the main() which is in another file and each time pass a reference of this instance to the rest of the methods in the program, or I can create a global Timing timing and state in .h file extern Timing timing. I chose the second option. However, i have some difficulties to connect between the global variable and my singleton pattern.
how to i create the instance of timing in the Timing.cpp file?
i tried Timing timing = CreateInstance(), however this doesn’t work..
i can’t create the instance in the main() because then i will be implementing the first option..
do i need to write in main timing.CreateInstance() ?
i hope i explained my self clearly.
thanks for your help

  • 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-16T18:38:02+00:00Added an answer on May 16, 2026 at 6:38 pm

    You don’t need to create the instance.

    Change the name of CreateInstance() to GetInstance().

    Then, when you want to access your timing object, call GetInstance. The singleton will be created the first time you access it.

    You won’t be implementing the first option if you do this because you won’t keep hold of the return value from GetInstance in a variable. You’ll just use it straight away.

    Don’t do first option and keep hold of t:

    Timing* t = Timing::GetInstance();
    t->DoSomething();
    

    Do the following when you want to use your singleton.

    Timing::GetInstance()->DoSomething();
    

    (That’s the basic guts of the answer anyway – technically, you can hold on to t for a while, and as long as you don’t keep passing it as parameters into every function that want it, you’re doing option 2)

    That’ll be sufficient for the task you’re doing.

    In production code, life times of singleton often need better control. To do that, I’d recommend having three functions:

    Timing::CreateInstance()
    Timing::GetInstance()
    Timing::DestroyInstance()
    

    Call Create and Destroy from the start and end of your main function. But, BE AWARE, you need to be careful that no code (even that in destructors) will access GetInstance() after you have called DestroyInstance(). I’d recommend you at least put an assert in the GetInstance function to:

    assert(_singleInstance != 0)
    return _singleInstance;
    

    And, in DestroyInstance do:

    delete _singleInstance;
    _singleInstance = 0;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.