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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:44:44+00:00 2026-05-16T22:44:44+00:00

I would like to know what is the difference between static variables in a

  • 0

I would like to know what is the difference between static variables in a header file vs one declared in a class.

When a static variable is declared in a header file is its scope limited to .h file or across all units. Generally, a static variable is initialized in a .cpp file when declared in a class, right? Does that mean a static variable scope is limited to two compilation units?

  • 1 1 Answer
  • 2 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-16T22:44:45+00:00Added an answer on May 16, 2026 at 10:44 pm

    Excuse me when I answer your questions out-of-order, it makes it easier to understand this way.

    When static variable is declared in a header file is its scope limited to .h file or across all units.

    There is no such thing as a "header file scope". The header file gets included into source files. The translation unit is the source file including the text from the header files. Whatever you write in a header file gets copied into each including source file.

    As such, a static variable declared in a header file is like a static variable in each individual source file.

    Since declaring a variable static this way means internal linkage, every translation unit #includeing your header file gets its own, individual variable (which is not visible outside your translation unit). This is usually not what you want.

    I would like to know what is the difference between static variables in a header file vs declared in a class.

    In a class declaration, static means that all instances of the class share this member variable; i.e., you might have hundreds of objects of this type, but whenever one of these objects refers to the static (or "class") variable, it’s the same value for all objects. You could think of it as a "class global".

    Also generally static variable is initialized in .cpp file when declared in a class right ?

    Yes, one (and only one) translation unit must initialize the class variable.

    So that does mean static variable scope is limited to 2 compilation units ?

    As I said:

    • A header is not a compilation unit,
    • static means completely different things depending on context.

    Global static limits scope to the translation unit. Class static means global to all instances.

    PS: Check the last paragraph of Chubsdad’s answer, about how you shouldn’t use static in C++ for indicating internal linkage, but anonymous namespaces. (Because he’s right. 😉 )

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

Sidebar

Related Questions

I would like to know difference between static variables and global variables in terms
I would like to know what is the exact difference between a class snippet
I would like to know what is the difference between static memory allocation and
I would like to know what is the difference between initializing a static member
I would like to know what the difference between these instructions is: MOV AX,
I would like to know what is the difference between Sleep mode and Deep
I would like to know is there any difference in performance between these two
I would like to know if there is a difference in speed between computing
I would like to know the difference between KEEP and UNCATLG. As per my
I would like to know the difference between Struts 1.x and Struts 2.x

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.