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

  • Home
  • SEARCH
  • 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 8792037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:55:29+00:00 2026-06-13T22:55:29+00:00

I have a question related to the static class member in C++. Based on

  • 0

I have a question related to the static class member in C++. Based on my understanding of C++, the static class number is supposed to exist before the class’s instance is created. It is possible to initialize the const static member variable, but for the non-const static member we cannot initialize it within the class. Therefore, my question is where we should initialize the non-const static class. It seems to me that the only stage for the non-const static class is before the main program is run as the following codes illustrate:

    using namespace std;
    class C
    {
    public:
        static int Value;

    };

    int C::Value = 2;

    int main()
    {
        // int C::Value = 2; //ERROR!
        cout<<C::Value<<endl; 
        return 0;
    }

Are there other ways to initialize it? Thanks!

  • 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-13T22:55:30+00:00Added an answer on June 13, 2026 at 10:55 pm

    Non-local objects in C++ program can be initialized statically and dynamically. In simple terms, static initialization is trivial C-style initialization with constant expressions that is essentially performed at compile-time (and, therefore, generates no code). Meanwhile dynamic initialization is initialization that involves some non-trivial actions that have to be performed at run time.

    You can assume that statically initialized objects begin their life in already initialized state. I.e. conceptually they are instantly initialized when your program starts.

    When it comes to dynamic initialization time and order, static class members are treated the same way as any other namespace-scope object. The language does not guarantee that all objects with static storage duration are initialized before main. Instead, the language guarantees that such static objects are initialized sometime before the first use of any function or object defined in the same translation unit. Static objects defined in the same translation unit are initialized in the order of their definition. The rules of dynamic initialization allow for the already mentioned “initialization order fiasco”.

    In your example – an int object initialized by an integral constant expression – static initialization will be used. It is safe to assume that this int object begins its life in already initialized state.

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

Sidebar

Related Questions

I have a question related to the following code snippet: class VarArgsTricky { static
I have a question related to C++ class member initialization. The following code illustrates
I have a question related to understanding of how python dictionaries work. I remember
I have following event class. I have a question related to the Property method
I have searched almost every possible question related to this topic but not able
This is a question related to RMI. i have 4 .class files an interface,it's
I have question related to what is done in SMS application is if i'm
I have a question related to this one . I don't want to do
I have a question related to this one : I'm trying to attach an
I have a modeling question related to profiles. Firstly, I have looked into using

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.