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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:10:11+00:00 2026-05-20T15:10:11+00:00

What is the rationale for not having static constructor in C++? If it were

  • 0

What is the rationale for not having static constructor in C++?

If it were allowed, we would be initializing all the static members in it, at one place in a very organized way, as:

//illegal C++
class sample
{
public:

    static int some_integer;
    static std::vector<std::string> strings;

    //illegal constructor!
    static sample()
    {
       some_integer = 100;
       strings.push_back("stack");
       strings.push_back("overflow");
    }
};

In the absense of static constructor, it’s very difficult to have static vector, and populate it with values, as shown above. static constructor elegantly solves this problem. We could initialize static members in a very organized way.

So why doesn’t’ C++ have static constructor? After all, other languages (for example, C#) has static constructor!

  • 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-20T15:10:12+00:00Added an answer on May 20, 2026 at 3:10 pm

    Using the static initialization order problem as an excuse to not introducing this feature to the language is and always has been a matter of status quo – it wasn’t introduced because it wasn’t introduced and people keep thinking that initialization order was a reason not to introduce it, even if the order problem has a simple and very straightforward solution.

    Initialization order, if people would have really wanted to tackle the problem, they would have had a very simple and straightforward solution:

    //called before main()
    
    int static_main() {
    
    ClassFoo();
    ClassBar();
    
    }
    

    with appropriate declarations:

    class ClassFoo {
     static int y;
      ClassFoo() {
       y = 1;
      }
    }
    
    class ClassBar {
      static int x;
      ClassBar() {
       x = ClassFoo::y+1;
      }
    }
    

    So the answer is, there is no reason it isn’t there, at least not a technical one.

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

Sidebar

Related Questions

Why is it not advisable to use JavaScript in JSP? One rationale that I
HI all, I'm having some difficulty understanding the rationale behind group by aggregation in
What is the rationale of not providing no-arg constructors in Wrapper Classes? I know
I am wondering what the rationale is behind having Java's Map.put(key, value) method overwrite
Can someone explain me the rationale behind having these two functions, instead of just
Following my previous question regarding the rationale behind extremely long functions, I would like
Possible Duplicate: What is the rationale behind having companion objects in Scala? Thanks for
I'm not used to having to take i18n stuff into account. I wrote this
Possible Duplicate: Java: Rationale of the Object class not being declared abstract Why is
Possible Duplicate: What's the rationale for null terminated strings? My question is very simple:

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.