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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:47:35+00:00 2026-05-27T02:47:35+00:00

In C and C++ all static variables are initialized by default to ZERO. This

  • 0

In C and C++ all static variables are initialized by default to ZERO.

This is not the case of static class data members. Why is that?

#include <iostream>
using namespace std;

int var;

class MyClass
{
public:
    static int classVar;
};
int MyClass::classVar = 0;  // Why I have to init it here?

int main(void)
{
    cout << ::var << endl;          // this is initalized to ZERO by default
    static int var;
    cout << var << endl;            // and this also is initalized to Zero
    cout << MyClass::classVar << endl;

    return 0;
}
  • 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-27T02:47:35+00:00Added an answer on May 27, 2026 at 2:47 am

    At class scope,

    int MyClass::classVar = 0;  // Why I have to init it here?
    

    is a definition and

    static int classVar;
    

    is a declaration, ie. a promise the variable will be defined somewhere: you must define exactly once the variables you declare.

    The rationale is that the class declaration will likely be included in multiple source files. Would a part of it be a definition, it would take place multiply: this is erroneous (exceptions are inline [member] functions).

    Note that according to value initialization rules, you can get along with

    int MyClass::classVar;  // Zero-initialized !
    

    as a definition.

    Variables declared at namespace scope are definitions too (unless they are extern qualified):

    int var;
    

    is a declaration, and a definition: if you put this into a header and include it in multiple translation units, you have an error (“multiply defined symbol”, or something along those lines).

    [Note that in C++ (and not in C), if the var above is const, it becomes automatically static and there is no violation of the One Definition Rule should it be put into a multiply included header. This goes slightly off topic, but feel free to ask details]

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

Sidebar

Related Questions

I have a class that has a fair amount of static variables, not all
All the initialized global/static variables will go to initialized data section . All the
All the member variables and member functions in my class ClassA are static. If
I'm doing code review and came across a class that uses all static methods.
The C++ specification says the default destructor deletes all non-static members. Nevertheless, I can't
For me, I usually make a global class with all members static. All other
Possible Duplicate: Why global and static variables are initialized to their default values? What
We say that global variables and static variables are always initialized to 0. Then
I have a header file that has some static variables for all of my
I have a class which have a static member. As I understand, all 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.