Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. static constructors ?

static constructors ?

Scheduled Pinned Locked Moved C / C++ / MFC
questioncsharpc++json
8 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    zildjohn01
    wrote on last edited by
    #1

    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()

    N A C C H 5 Replies Last reply
    0
    • Z zildjohn01

      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()

      N Offline
      N Offline
      Neagoe Gabriel
      wrote on last edited by
      #2

      [Message Deleted]

      1 Reply Last reply
      0
      • Z zildjohn01

        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()

        A Offline
        A Offline
        Anonymous
        wrote on last edited by
        #3

        #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; }

        J 1 Reply Last reply
        0
        • Z zildjohn01

          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()

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          No, but this[^] article explains how to emulate static constructors in C++. Christian Graus - Microsoft MVP - C++

          1 Reply Last reply
          0
          • A Anonymous

            #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; }

            J Offline
            J Offline
            Jorgen Sigvardsson
            wrote on last edited by
            #5

            That's not even close to what he wants. Good music: In my rosary[^]

            C 1 Reply Last reply
            0
            • J Jorgen Sigvardsson

              That's not even close to what he wants. Good music: In my rosary[^]

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              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++

              1 Reply Last reply
              0
              • Z zildjohn01

                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()

                C Offline
                C Offline
                cmk
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                • Z zildjohn01

                  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()

                  H Offline
                  H Offline
                  Hugo Gonzalez Castro
                  wrote on last edited by
                  #8

                  Maybe this article is useful for you: Static Constructor in C++

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups