This is trivial in C #, but in C ++ (native, Win32, Visual C ++) I don't see a solution. So, I have a class MyClass1 with a non-trivial constructor, and in MyClass2 I want to have a static member like 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 this code to initialize MyClass2 :: Field and call the constructor MyClass1 during this initialization. However, it seems that the compiler allocates memory only for class 1 and never calls the constructor, for example, if I do this:
MyClass1 MyClass2::Field1 = *(MyClass1 *)malloc(sizeof(MyClass1));
Is there any โofficialโ way in C ++ to initialize a static member of a class with a non-trivial constructor?
source share