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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:31:18+00:00 2026-05-25T15:31:18+00:00

Suppose you have a static global variable in your header file, and you use

  • 0

Suppose you have a static global variable in your header file, and you use this variable in your main.cpp.

// header.h
static int variableOne = 100;

//main.cpp
   .
   .
   cout << variableOne << endl;

Will main.cpp get its own copy of variableOne (although the value is still 100…)? Or am I mixing this concept with extern (I know that extern tells the compiler that variableOne is defined elsewhere in the project…)

Thank you.

  • 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-25T15:31:18+00:00Added an answer on May 25, 2026 at 3:31 pm

    1. What does it mean #include

    When you write #include "header.h" the compiler is basically just copying the whole content of the file instead of that line. In C++ an header file has no semantic meaning, it’s just used for textual substitution.

    So if you have

    ---- header.h ----
    static int variableOne = 100;
    
    ---- main.cpp ----
    #include "header.h"
    
    ...
    std::cout << variableOne << std::endl;
    

    for the compiler is exactly the same as

    ---- main.cpp ----
    static int variableOne = 100;
    
    ...
    std::cout << variableOne << std::endl;
    

    2. What does it mean static in that context

    A static variable is like a global for the lifetime (i.e. it’s constructed before main starts and it’s destroyed after main terminates) but is only visible inside the translation unit that defines it. So you can have the same static variable name used in different translation units and all those variables are distinct (each translation unit will see its own static variable).

    3. What happens when you put a static variable declaration in an header?

    The net result is that every translation unit that will include that header will get its own static variable with that name because it’s exactly the same as if the translation unit defined a static variable in the .cpp and not in the header file.

    The name will be the same for all of them but they will be different variables. Of course if the header file declares and initializes the variable then the initial value will be the same, but all those variables will be distinct and if for example one translation unit changes that variable, the change will not be seen by other translation units.

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

Sidebar

Related Questions

Suppose I have two functions which look like this: public static void myFunction1(int a,
Suppose I have this code: static void Main(string[] args) { var thread = new
Suppose I have a static variable declared inside a function in C. If I
Suppose I have two classes that both contain a static variable, XmlTag. The second
Suppose I have following code package memoryleak; public class MemoryLeak { public static int
Suppose I have template< unsigned int num > class SomeFunctionality { static unsigned int
Suppose I have a function that tries to protect a global counter using this
Suppose I have this code (pseudocode) class SomeClass { class Person { public static
Suppose I have this class: public class DispatcherService<T> { private static Action<T> Dispatcher; public
Suppose I have a Java class like this: public class Test { 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.