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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:08:42+00:00 2026-05-11T08:08:42+00:00

In C++, say you want to declare a global variable to be used by

  • 0

In C++, say you want to declare a global variable to be used by many. How do you do it?

I commonly use declare and define in cpp file, and then use extern in other cpp file (and not headers).

I don’t like this approach, and I am considering something along these lines:

In a header file:

some_file.h

Class MYGlobalClass {  };   MyGlobalClass& MyGlobalClassInstance() {    static MYGlobalClass instance;     return  instance;  } 

Edit

Consider in the following contexts:

  • can be used in multi-threaded applications
  • namespace pollution
  • may NOT necessery be a singleton, as many instances of this might be created

What are your thoughts, suggestions, new ideas?

  • 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. 2026-05-11T08:08:43+00:00Added an answer on May 11, 2026 at 8:08 am

    Declare it in one header file (using extern), and define it in one .cpp (or whatever other extension) file. You may use a function and return a reference to a static variable like you showed to circumvent problems with construction order relative to other such namespace scope variables in other .cpp files. But remember that won’t protect you from destruction order problems – which is in the exact reverse order from construction (these things are called ‘static initialization order fiasco’. If you use a function like yours and put it into headers, make it inline to make the redefinition of the function valid when it is included into multiple .cpp files (logically, the function is still only apparent once, because the static in it will only exist once, not separately for each file it is included into). Alternatively just declare it in a header but define it in one .cpp file (but then, remove the inline from it!).

    inline A& getA() { static A a; return a; } 

    The potential problems with destruction order can be circumvented by using new:

    inline A& getA() { static A *a = new A; return *a; } 

    The destructor of it, however, will never be called then. If you need thread safety, you should add a mutex that protects against multiple accesses. boost.thread probably has something for that.

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

Sidebar

Related Questions

Let's say I declare a variable: String a = test; And I want to
Say I want to disassemble lines m-n of file x, where file x is
Let's say I want to declare a function that has this java signature: public
Let's say I would want to use the frameScript method to add some stop
Let's say I have <span class=1>hello</span> And I want to declare: span.1 { /*
First off, I want to say there is no use case for this. The
I want to pause the timer thread....or i can say want to put it
Say I want to draw a Ball in the scene and here are two
Say you want to install a library lib-a which has dependencies dep-1 and dep-2
Say I want to write a function like this: int get_some_int(string index) { ...perform

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.