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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:04:03+00:00 2026-06-06T21:04:03+00:00

Why must static data member initialization be outside the class? class X { public:

  • 0

Why must static data member initialization be outside the class?

class X
{
public:
      int normalValue = 5; //NSDMI
      static int i;
};

int X::i = 0;

Why is the static data member (here “i”) only a declaration, not a definition?

  • 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-06-06T21:04:04+00:00Added an answer on June 6, 2026 at 9:04 pm

    It’s important to distinguish the initializer which says what its initial value is, and the definition. This modified code is valid, with the initializer in the class definition:

    class X
    {
    public:
      int normalValue = 5;
      static const int i = 0;       // declaration, with initializer
    };
    
    const int X::i;                 // definition
    

    i.e. What must be outside the class is a definition, not the initialization.

    That’s because a variable must have an address in memory (unless it’s only used in limited situations, such as in compile-time constant expressions.)

    A non-static member variable exists inside the object it is a member of, so its address depends on the address of the object that contains it. Every time you create a new X you also create a new X::normalValue variable. The non-static data member’s lifetime begins with the class’ constructor. NSDMI syntax doesn’t have anything to do with the variable’s address in memory, it just allows you to provide an initial value in one place, instead of repeating it in every constructor with an explicit constructor initializer list.

    On the other hand, a static member variable is not contained within an instance of the class, it exists independently of any single instance and exists from the start of the program, at a fixed address. In order for a static member variable (or any other global object) to get a unique address the linker must see exactly one definition of the static variable, in exactly one object file, and assign it an address.

    Because a static variable needs exactly one definition in exactly one object file, it doesn’t make sense to allow that definition to be provided in the class, since class definitions typically exist in header files and are included in multiple object files. So although you can provide an initializer in the class, you still need to define the static data member somewhere.

    You can also look at it like declaring an extern variable:

    namespace X {
      extern int i;
    }
    

    This declares the variable, but there must be a definition somewhere in the program:

    int X::i = 0;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am updating the data by the following method class xxxxxx { public static
I have the following class: public class Data { static public SqlDataReader ExecutSql(string sql)
I have static data in a single table which I must serialize. I have
I have a class declaration(.h file) like so: struct MyClass { static const uint32_t
I currently have the following code: public static int currentTimeMillis() { long millisLong =
I am creating a static table view (must be compatible with iOS 4 -
In this static function in Java, why must <K, V> be repeated in line
We have a coding standard that says all shared (static) fields and methods must
Must be missing something really obvious here but how do you change the size
take two following classes: class Test1{ public: Test1()=default; Test1(char in1,char in2):char1(in1),char2(in2){} char char1; char

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.