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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:01:49+00:00 2026-06-18T00:01:49+00:00

It is trivial in C#, but in C++ (native, Win32, Visual C++) I don’t

  • 0

It is trivial in C#, but in C++ (native, Win32, Visual C++) I don’t see solution. So, I have class MyClass1 with non-trivial constructor, and in MyClass2 I want to have static member of type MyClass1:

MyClass1.h:

class MyClass1 
{
   public MyClass1(type1 arg1, type2 arg2);
}

MyClass2.h:

class MyClass2
{
public:
   static MyClass1 Field1;
}

And MyClass2.cpp:

MyClass1 MyClass2::Field1(arg1, arg2);

I expect that such code will initialize MyClass2::Field and call MyClass1 constructor during this initialization. However, it looks like compiler allocates memory for Class1 only, and never calls constructor, like if I do this:

MyClass1 MyClass2::Field1 = *(MyClass1 *)malloc(sizeof(MyClass1));

Is there any “official” way in C++ to initialize static class member with nontrivial 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-06-18T00:01:50+00:00Added an answer on June 18, 2026 at 12:01 am

    You may be encountering the Static Initialization Order Fiasco. Static variables of class or namespace scope are initialized before main() is executed, but the order of initialization is dependent on link-time factors.

    To solve the problem, use the Construct on First Use Idiom, which takes advantage of the fact that function-scope statics are initialized at the time the function is first called.

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

Sidebar

Related Questions

It seemed a trivial matter at the beginning but so far I have not
I have code which is trivial but only works in IE not Firefox. $(document).ready(function(){
This is really trivial but I'm getting an error I didn't expect. I have
This is very trivial, but Czech language (my native) doesn't distinguish between implicit and
Should be kind of trivial but say I have two jar files in libA.jar
This may seem trivial but I have no idea how and the lack of
I have problem that seems to be rather trivial but I was not able
This may sound trivial but i have a problem with setting selected item in
I have a large application written in native C++. I also have a class
My problem seem's trivial but I havn't found any good solution to this. First

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.