static constructors ?
-
hello all, i have a question: c# has the feature of 'static constructors' meaning that the static constructor is guaranteed to be called before any part of the rest of a class is used or instantiated. i.e. the following code:
class MyClass { private static int mydata[]; public static MyClass() { MyClass::mydata = new int[20]; } public static doSomething() { // ... } }
before DoSomething is accessed, the test() static consturctor is guaranteed to be called. any equivalent of this in c++ ? (alternative to doing everything in the WinMain() or the _main() -
hello all, i have a question: c# has the feature of 'static constructors' meaning that the static constructor is guaranteed to be called before any part of the rest of a class is used or instantiated. i.e. the following code:
class MyClass { private static int mydata[]; public static MyClass() { MyClass::mydata = new int[20]; } public static doSomething() { // ... } }
before DoSomething is accessed, the test() static consturctor is guaranteed to be called. any equivalent of this in c++ ? (alternative to doing everything in the WinMain() or the _main()[Message Deleted]
-
hello all, i have a question: c# has the feature of 'static constructors' meaning that the static constructor is guaranteed to be called before any part of the rest of a class is used or instantiated. i.e. the following code:
class MyClass { private static int mydata[]; public static MyClass() { MyClass::mydata = new int[20]; } public static doSomething() { // ... } }
before DoSomething is accessed, the test() static consturctor is guaranteed to be called. any equivalent of this in c++ ? (alternative to doing everything in the WinMain() or the _main()#pragma once class DoMuie { public: DoMuie(void); ~DoMuie(void); static int MuieYesOrNo(void); }; #include ".\domuie.h" DoMuie::DoMuie(void) { } DoMuie::~DoMuie(void) { } int DoMuie::MuieYesOrNo(void) { DoMuie(); //static function calling class constructor // you can decalre static variables in constructor to do.... muie return 0; }
-
hello all, i have a question: c# has the feature of 'static constructors' meaning that the static constructor is guaranteed to be called before any part of the rest of a class is used or instantiated. i.e. the following code:
class MyClass { private static int mydata[]; public static MyClass() { MyClass::mydata = new int[20]; } public static doSomething() { // ... } }
before DoSomething is accessed, the test() static consturctor is guaranteed to be called. any equivalent of this in c++ ? (alternative to doing everything in the WinMain() or the _main() -
#pragma once class DoMuie { public: DoMuie(void); ~DoMuie(void); static int MuieYesOrNo(void); }; #include ".\domuie.h" DoMuie::DoMuie(void) { } DoMuie::~DoMuie(void) { } int DoMuie::MuieYesOrNo(void) { DoMuie(); //static function calling class constructor // you can decalre static variables in constructor to do.... muie return 0; }
That's not even close to what he wants. Good music: In my rosary[^]
-
That's not even close to what he wants. Good music: In my rosary[^]
LOL - I stared at it for a while, and just assumed my C++ was too rusty, because I could not see how it helped :-) Christian Graus - Microsoft MVP - C++
-
hello all, i have a question: c# has the feature of 'static constructors' meaning that the static constructor is guaranteed to be called before any part of the rest of a class is used or instantiated. i.e. the following code:
class MyClass { private static int mydata[]; public static MyClass() { MyClass::mydata = new int[20]; } public static doSomething() { // ... } }
before DoSomething is accessed, the test() static consturctor is guaranteed to be called. any equivalent of this in c++ ? (alternative to doing everything in the WinMain() or the _main()Aside from creating an elaborate framework, or initializing all globals/statics in an init function your only real option is through the use of #pragma init_seg(). The pragma specifies when the global/static variables in a source file (.c/.cpp) are to be initialized. There are 3 defined groups: compiler, lib, user that are initialized in that order. e.g. Given (in various .h files): extern int g_a; class C {...}; // uses g_a extern C g_c; fileA.cpp (source with g_a): #pragma init_seg(lib) int g_a = 0; fileC.cpp (source for class C): #pragma init_seg(user) C g_c(); Normally it would be a crap shoot on whether g_a is initialized before g_c. However, with the pragma's, the compiler will make sure g_a = 0 before g_c is constructed. ...cmk Save the whales - collect the whole set
-
hello all, i have a question: c# has the feature of 'static constructors' meaning that the static constructor is guaranteed to be called before any part of the rest of a class is used or instantiated. i.e. the following code:
class MyClass { private static int mydata[]; public static MyClass() { MyClass::mydata = new int[20]; } public static doSomething() { // ... } }
before DoSomething is accessed, the test() static consturctor is guaranteed to be called. any equivalent of this in c++ ? (alternative to doing everything in the WinMain() or the _main()Maybe this article is useful for you: Static Constructor in C++