some thoughts about static class member type CX of class CX

Discussion in 'C++' started by .rhavin grobert, Aug 21, 2007.

  1. i have (do try to have?) the following...

    [1] & [2] = breakpoints in debugger

    // ----------------------------------------------------------------
    // cx.h
    class CX {
    public:
    CX(CX* pcx = NULL);
    virtual ~CX();

    int iFoo;
    int fnFoo();

    private:
    static CX s_cx;
    };



    // ----------------------------------------------------------------
    // cx.cpp

    /*[1]*/ CX CX::s_cx(0);

    /*[2]*/ CX::CX(CX* pcx) {
    //do something...
    }

    //________________________________________


    i always thougt that the initialisation of the static class member at
    [1] will be called before any object is constructed [2], but in my
    implementation (the example is just an abstraction) there are several
    call of the ctor *before* the static member is initialized.

    the class in my implementation is something like this:

    it consists of a private std::vector of struct {void* p; int iID; /
    *some other data ....*/} and some fuctions for manipulating items
    (normally by iID). the whole class shall have a static class member of
    its own class to register all class objects, that is: the constructor
    calls a static function "register(this, iID);" and the static function
    "register" fills the static class vector.

    the problem is that the ctor will be called (4 times for 4 different
    vectors, breakpoint [2]) and the static class vector if filled with 4
    structs *and then* its initialisation is called (breakpoint [1]) and
    the vector is cleared().

    how do i force the initialisation of all static class mebers *before*
    any constructor is called?
     
    .rhavin grobert, Aug 21, 2007
    #1
    1. Advertising

  2. On Aug 21, 8:28 pm, ".rhavin grobert" <> wrote:
    > i have (do try to have?) the following...
    >
    > [1] & [2] = breakpoints in debugger
    >
    > // ----------------------------------------------------------------
    > // cx.h
    > class CX {
    > public:
    > CX(CX* pcx = NULL);
    > virtual ~CX();
    >
    > int iFoo;
    > int fnFoo();
    >
    > private:
    > static CX s_cx;
    >
    > };
    >
    > // ----------------------------------------------------------------
    > // cx.cpp
    >
    > /*[1]*/ CX CX::s_cx(0);
    >
    > /*[2]*/ CX::CX(CX* pcx) {
    > //do something...
    >
    > }
    >
    > //________________________________________
    >
    > i always thougt that the initialisation of the static class member at
    > [1] will be called before any object is constructed [2], but in my
    > implementation (the example is just an abstraction) there are several
    > call of the ctor *before* the static member is initialized.
    >
    > the class in my implementation is something like this:
    >
    > it consists of a private std::vector of struct {void* p; int iID; /
    > *some other data ....*/} and some fuctions for manipulating items
    > (normally by iID). the whole class shall have a static class member of
    > its own class to register all class objects, that is: the constructor
    > calls a static function "register(this, iID);" and the static function
    > "register" fills the static class vector.
    >
    > the problem is that the ctor will be called (4 times for 4 different
    > vectors, breakpoint [2]) and the static class vector if filled with 4
    > structs *and then* its initialisation is called (breakpoint [1]) and
    > the vector is cleared().
    >
    > how do i force the initialisation of all static class mebers *before*
    > any constructor is called?


    Use static member function instead of static member object. Refer
    http://www.parashift.com/c -faq-lite/ctors.html#faq-10.15 for
    details.

    -N
     
    Neelesh Bodas, Aug 21, 2007
    #2
    1. Advertising

  3. ..rhavin grobert wrote:
    > [..]
    > how do i force the initialisation of all static class mebers *before*
    > any constructor is called?


    This is (a) in the FAQ, (b) has recently been discussed again. So,
    see the FAQ, search the archives (using Google Groups, for example).
    Essentially, your question is "how do I call a constructor of CX
    before any call to a constructor of CX". Rebuttal?

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 21, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sam
    Replies:
    4
    Views:
    491
    The Directive
    Jan 13, 2004
  2. Steven T. Hatton
    Replies:
    1
    Views:
    6,758
    Siemel Naran
    Apr 19, 2004
  3. Siemel Naran
    Replies:
    4
    Views:
    839
    Micah Cowan
    Jan 12, 2005
  4. Rajan
    Replies:
    4
    Views:
    335
    red floyd
    May 27, 2005
  5. dolphin
    Replies:
    3
    Views:
    1,402
    Pete Becker
    Dec 5, 2007
Loading...

Share This Page