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 and destructors in C++

static constructors and destructors in C++

Scheduled Pinned Locked Moved C / C++ / MFC
c++
22 Posts 7 Posters 2 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.
  • N Nish Nishant

    [removed]

    N Offline
    N Offline
    Nish Nishant
    wrote on last edited by
    #2

    [removed]

    T N 2 Replies Last reply
    0
    • N Nish Nishant

      [removed]

      T Offline
      T Offline
      Tim Smith
      wrote on last edited by
      #3

      A static constructor without a this? What would you be constructing? Tim Smith Descartes Systems Sciences, Inc.

      1 Reply Last reply
      0
      • N Nish Nishant

        [removed]

        T Offline
        T Offline
        Tim Smith
        wrote on last edited by
        #4

        Ah, I see. What people have done in the past is create a class whos member variable is the variable to be initialized at program start. Then just create one global instance of that class. Tim Smith Descartes Systems Sciences, Inc.

        T 1 Reply Last reply
        0
        • N Nish Nishant

          [removed]

          N Offline
          N Offline
          Nish Nishant
          wrote on last edited by
          #5

          [removed]

          T R 2 Replies Last reply
          0
          • T Tim Smith

            Ah, I see. What people have done in the past is create a class whos member variable is the variable to be initialized at program start. Then just create one global instance of that class. Tim Smith Descartes Systems Sciences, Inc.

            T Offline
            T Offline
            Tim Smith
            wrote on last edited by
            #6

            DOH, stupid me. Create any class and create a global instance of it. Use that to perform any program start initializations. Tim Smith Descartes Systems Sciences, Inc.

            1 Reply Last reply
            0
            • N Nish Nishant

              [removed]

              M Offline
              M Offline
              Michael Dunn
              wrote on last edited by
              #7

              There is no such thing. You'd do something like:

              // In foo.h:
              class C
              {
              public:
              static int m_stat_var;
              static int stat_func() { return 1; }
              };

              // In foo.cpp:
              int C::m_stat_var = C::stat_func();

              --Mike-- "There are only a limited number of jobs where they will ask to see the sausage. Most of them are in movies."  -- Christian Graus, 2/11/2002 My really out-of-date homepage Sonork - 100.10414 AcidHelm Big fan of Alyson Hannigan.

              N 1 Reply Last reply
              0
              • N Nish Nishant

                [removed]

                A Offline
                A Offline
                Atul Dharne
                wrote on last edited by
                #8

                Static Constructor...The idea is kinda weird :-)..How are they implemented in C#? Atul Sonork 100.13714 netdiva

                1 Reply Last reply
                0
                • M Michael Dunn

                  There is no such thing. You'd do something like:

                  // In foo.h:
                  class C
                  {
                  public:
                  static int m_stat_var;
                  static int stat_func() { return 1; }
                  };

                  // In foo.cpp:
                  int C::m_stat_var = C::stat_func();

                  --Mike-- "There are only a limited number of jobs where they will ask to see the sausage. Most of them are in movies."  -- Christian Graus, 2/11/2002 My really out-of-date homepage Sonork - 100.10414 AcidHelm Big fan of Alyson Hannigan.

                  N Offline
                  N Offline
                  Nish Nishant
                  wrote on last edited by
                  #9

                  [removed]

                  M R A H 4 Replies Last reply
                  0
                  • N Nish Nishant

                    [removed]

                    M Offline
                    M Offline
                    Michael Dunn
                    wrote on last edited by
                    #10

                    That's Just The Way It's Done. :) It's similar to the situation when you write "extern int n;" in a header file. You still need to actually declare the variable "int n;" in a .cpp file. --Mike-- "There are only a limited number of jobs where they will ask to see the sausage. Most of them are in movies."  -- Christian Graus, 2/11/2002 My really out-of-date homepage Sonork - 100.10414 AcidHelm Big fan of Alyson Hannigan.

                    N 1 Reply Last reply
                    0
                    • M Michael Dunn

                      That's Just The Way It's Done. :) It's similar to the situation when you write "extern int n;" in a header file. You still need to actually declare the variable "int n;" in a .cpp file. --Mike-- "There are only a limited number of jobs where they will ask to see the sausage. Most of them are in movies."  -- Christian Graus, 2/11/2002 My really out-of-date homepage Sonork - 100.10414 AcidHelm Big fan of Alyson Hannigan.

                      N Offline
                      N Offline
                      Nish Nishant
                      wrote on last edited by
                      #11

                      [removed]

                      M 1 Reply Last reply
                      0
                      • N Nish Nishant

                        [removed]

                        M Offline
                        M Offline
                        Michael Dunn
                        wrote on last edited by
                        #12

                        Well, I can't really answer that since I don't know C#, so don't know what you mean by a static constructor. :( --Mike-- "There are only a limited number of jobs where they will ask to see the sausage. Most of them are in movies."  -- Christian Graus, 2/11/2002 My really out-of-date homepage Sonork - 100.10414 AcidHelm Big fan of Alyson Hannigan.

                        1 Reply Last reply
                        0
                        • N Nish Nishant

                          [removed]

                          R Offline
                          R Offline
                          Ravi Bhavnani
                          wrote on last edited by
                          #13

                          Looks like a factory pattern is what you want. /ravi "There is always one more bug..." http://www.ravib.com ravib@ravib.com

                          N N 2 Replies Last reply
                          0
                          • R Ravi Bhavnani

                            Looks like a factory pattern is what you want. /ravi "There is always one more bug..." http://www.ravib.com ravib@ravib.com

                            N Offline
                            N Offline
                            Nish Nishant
                            wrote on last edited by
                            #14

                            [removed]

                            R 1 Reply Last reply
                            0
                            • N Nish Nishant

                              [removed]

                              R Offline
                              R Offline
                              Ravi Bhavnani
                              wrote on last edited by
                              #15

                              One use of a factory design pattern is to ensure that objects are properly initialized during construction. I assume this is what you'd like to accomplish via the static constructor. To implement a factory pattern for the class CFoo, do the following:

                              1. Make CFoo's constructor protected, thereby preventing anyone from doing CFoo* pFoo = new CFoo(); which could create an uninitialized instance.
                              2. Implement the static method CFoo* CFoo::Create(); that serves up a properly initialized object. The static method can set the class's static member is required.

                              You should browse thru the gang of 4 book. I think you'll enjoy it. /ravi "There is always one more bug..." http://www.ravib.com ravib@ravib.com

                              1 Reply Last reply
                              0
                              • N Nish Nishant

                                [removed]

                                R Offline
                                R Offline
                                Ravi Bhavnani
                                wrote on last edited by
                                #16

                                Nish [BusterBoy] wrote: Why am I re-declaring the static int You're not redeclaring it. The .h file declares it - the .cpp allocates storage for it. If you omitted it from the .cpp file you'd get an unresolved reference link error. /ravi "There is always one more bug..." http://www.ravib.com ravib@ravib.com

                                N 1 Reply Last reply
                                0
                                • N Nish Nishant

                                  [removed]

                                  A Offline
                                  A Offline
                                  Atul Dharne
                                  wrote on last edited by
                                  #17

                                  You are not redeclaring it but you are just declaring it. As it is static it resides even if your class object has not been constructed. It is like using static variables in functions or global vars. IMO the compiler initialises them to zero if you don't do the essentials. So here just after your class def you should initialize it otherwise the compiler spews errors. Atul Sonork 100.13714 netdiva

                                  1 Reply Last reply
                                  0
                                  • R Ravi Bhavnani

                                    Nish [BusterBoy] wrote: Why am I re-declaring the static int You're not redeclaring it. The .h file declares it - the .cpp allocates storage for it. If you omitted it from the .cpp file you'd get an unresolved reference link error. /ravi "There is always one more bug..." http://www.ravib.com ravib@ravib.com

                                    N Offline
                                    N Offline
                                    Nish Nishant
                                    wrote on last edited by
                                    #18

                                    Ravi Bhavnani wrote: If you omitted it from the .cpp file you'd get an unresolved reference link error. Yes I did get that error :-O :-O :-O or if you want it in Ravi-talk pinky pinky :-) Nish Nish was here, now Nish has gone; He left his soul, to turn you on; Those who knew Nish, knew him well; Those who didn't, can go to hell. I like to :jig: on the Code Project Sonork ID 100.9786 voidmain www.busterboy.org

                                    R 1 Reply Last reply
                                    0
                                    • N Nish Nishant

                                      Ravi Bhavnani wrote: If you omitted it from the .cpp file you'd get an unresolved reference link error. Yes I did get that error :-O :-O :-O or if you want it in Ravi-talk pinky pinky :-) Nish Nish was here, now Nish has gone; He left his soul, to turn you on; Those who knew Nish, knew him well; Those who didn't, can go to hell. I like to :jig: on the Code Project Sonork ID 100.9786 voidmain www.busterboy.org

                                      R Offline
                                      R Offline
                                      Ravi Bhavnani
                                      wrote on last edited by
                                      #19

                                      I think I said "picky picky". :) /ravi "There is always one more bug..." http://www.ravib.com ravib@ravib.com

                                      1 Reply Last reply
                                      0
                                      • R Ravi Bhavnani

                                        Looks like a factory pattern is what you want. /ravi "There is always one more bug..." http://www.ravib.com ravib@ravib.com

                                        N Offline
                                        N Offline
                                        Navier
                                        wrote on last edited by
                                        #20

                                        Seems to me like a Singleton pattern would do the job better. BTW the standard (well Nov96 draft at least) says a constructor can't have a static modifier (12.1 para 4) if you want official confirmation. i1.2sqrt(u).bcos(ur)sec(c) but b4.isqrt(u).ru/16

                                        R 1 Reply Last reply
                                        0
                                        • N Navier

                                          Seems to me like a Singleton pattern would do the job better. BTW the standard (well Nov96 draft at least) says a constructor can't have a static modifier (12.1 para 4) if you want official confirmation. i1.2sqrt(u).bcos(ur)sec(c) but b4.isqrt(u).ru/16

                                          R Offline
                                          R Offline
                                          Ravi Bhavnani
                                          wrote on last edited by
                                          #21

                                          I think the Singleton pattern is better suited when you want to expose methods from the one and only instance of a class. But I agree that the ClassFactory pattern can do more than serve up a single class. In fact, it's power lies in the fact that it can serve up specializations of a general base class. /ravi "There is always one more bug..." http://www.ravib.com ravib@ravib.com

                                          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