where to initialize a static class variable

Discussion in 'C++' started by lou zion, Jun 9, 2005.

  1. lou zion

    lou zion Guest

    hello all,

    i'm trying to wrap my head around this one, but something's missing. I'm
    trying to create a class where all instances share a common variable that is
    an instance of a different class. so, thought i'd use a static variable.
    but, where can i initialize it?

    myclass.h
    -------------------
    class MainClass()
    {
    declarations...

    static OtherClass OC; // oc should be the same across all classes.
    changes by one class instance change it in all instances
    ...
    }

    myclass.cpp
    ---------------------
    #include myclass.h

    //i want to initialize my static variable OC here so all classes can see it,
    but how?

    MainClass::MainClass(A1,A2,...): var1(A1), var2(A2)... // can't
    initialize it here, cuz that's per instance, correct?
    {
    ...
    }

    in a class module, when is code that is outside the class definition
    executed? on the first instance? how and where can i initialize OC?

    thanks!

    lou
     
    lou zion, Jun 9, 2005
    #1
    1. Advertising

  2. lou zion

    lou zion Guest

    > myclass.h
    > -------------------
    > class MainClass()
    > {
    > declarations...
    >
    > static OtherClass OC; // oc should be the same across all classes.
    > changes by one class instance change it in all instances
    > ...
    > }


    a new complication...

    OtherClass is now MainClass. the idea is this: there is a default setting
    for each variable in the class, but the defaults can be changed by the
    class. i want every instance to have access to the current default settings
    to retrieve them or change them. so, the above line now reads:

    static MainClass OC; // OC should be the same across all classes.

    thanks again!

    lou
     
    lou zion, Jun 9, 2005
    #2
    1. Advertising

  3. lou zion wrote:
    > i'm trying to wrap my head around this one, but something's missing. I'm
    > trying to create a class where all instances share a common variable that is
    > an instance of a different class. so, thought i'd use a static variable.
    > but, where can i initialize it?
    >
    > myclass.h
    > -------------------
    > class MainClass()
    > {
    > declarations...
    >
    > static OtherClass OC; // oc should be the same across all classes.
    > changes by one class instance change it in all instances
    > ...
    > }
    >
    > myclass.cpp
    > ---------------------
    > #include myclass.h
    >
    > //i want to initialize my static variable OC here so all classes can see it,
    > but how?


    #include otherclass.h

    OtherClass MainClass::OC;

    >
    > MainClass::MainClass(A1,A2,...): var1(A1), var2(A2)... // can't
    > initialize it here, cuz that's per instance, correct?
    > {
    > ...
    > }
    >
    > in a class module, when is code that is outside the class definition
    > executed?


    Before 'main' function is called.

    > on the first instance?


    Before it, unless your "first instance" is itself static. Then you
    need to worry about the order in which they are initialised. Read
    about "static object initialisation fiasco".

    > how and where can i initialize OC?


    You initialise it as appropriate (according to OtherClass constructors)
    somewhere in one of the translation units.

    BTW, what C++ book are you reading that doesn't talk about static
    data members and how they are initialised?

    V
     
    Victor Bazarov, Jun 9, 2005
    #3
  4. lou zion wrote:
    >>myclass.h
    >>-------------------
    >>class MainClass()
    >>{
    >> declarations...
    >>
    >> static OtherClass OC; // oc should be the same across all classes.
    >>changes by one class instance change it in all instances
    >> ...
    >>}

    >
    >
    > a new complication...
    >
    > OtherClass is now MainClass. the idea is this: there is a default setting
    > for each variable in the class, but the defaults can be changed by the
    > class. i want every instance to have access to the current default settings
    > to retrieve them or change them. so, the above line now reads:
    >
    > static MainClass OC; // OC should be the same across all classes.


    This essentially changes only the class names in my response.

    V
     
    Victor Bazarov, Jun 9, 2005
    #4
  5. lou zion

    Howard Guest

    "Victor Bazarov" <> wrote in message
    news:qU0qe.82052$01.us.to.verio.net...
    > lou zion wrote:
    >>>myclass.h
    >>>-------------------
    >>>class MainClass()
    >>>{
    >>> declarations...
    >>>
    >>> static OtherClass OC; // oc should be the same across all classes.
    >>> changes by one class instance change it in all instances
    >>> ...
    >>>}

    >>
    >>
    >> a new complication...
    >>
    >> OtherClass is now MainClass. the idea is this: there is a default
    >> setting for each variable in the class, but the defaults can be changed
    >> by the class. i want every instance to have access to the current
    >> default settings to retrieve them or change them. so, the above line now
    >> reads:
    >>
    >> static MainClass OC; // OC should be the same across all classes.

    >
    > This essentially changes only the class names in my response.
    >
    > V


    It changes a lot more than that, if I understand what he's saying. He's
    said that OC is a member of MainClass. Then he said that, instead of OC
    being of the type OtherClass, it's actually of the type MainClass. Well,
    that's just not possible! You can't contain a class inside itself, even if
    it's a static member, can you? The static member, after all, would now have
    itself as a member! Or did I miss something?

    Perhaps what is needed is just a single global object, instead of a static
    member?

    -Howard
     
    Howard, Jun 9, 2005
    #5
  6. "lou zion" <> wrote in message
    news:...
    > hello all,
    >
    > i'm trying to wrap my head around this one, but something's missing. I'm
    > trying to create a class where all instances share a common variable that
    > is an instance of a different class. so, thought i'd use a static
    > variable. but, where can i initialize it?
    >
    > myclass.h
    > -------------------
    > class MainClass()
    > {
    > declarations...
    >
    > static OtherClass OC; // oc should be the same across all classes.
    > changes by one class instance change it in all instances
    > ...
    > }
    >
    > myclass.cpp
    > ---------------------
    > #include myclass.h
    >
    > //i want to initialize my static variable OC here so all classes can see
    > it, but how?
    >
    >


    Formally speaking you should define a static class data member anywhere in
    the namespace enclosing the class definition and ensure the uniqueness of
    the definition. Of course you should not do it in a header but it may be any
    ..cpp file which is compiled and linked with your project. Any other code
    needs only to view a variable declaration which is done in your class
    definition and included as a header file. Finally it is up to linker to
    connect a static variable with any references to it in any compiled files.

    Regards,
    Janusz
     
    Janusz Szpilewski, Jun 9, 2005
    #6
  7. Howard wrote:
    > "Victor Bazarov" <> wrote in message
    > news:qU0qe.82052$01.us.to.verio.net...
    >
    >>lou zion wrote:
    >>
    >>>>myclass.h
    >>>>-------------------
    >>>>class MainClass()
    >>>>{
    >>>> declarations...
    >>>>
    >>>> static OtherClass OC; // oc should be the same across all classes.
    >>>>changes by one class instance change it in all instances
    >>>> ...
    >>>>}
    >>>
    >>>
    >>>a new complication...
    >>>
    >>>OtherClass is now MainClass. the idea is this: there is a default
    >>>setting for each variable in the class, but the defaults can be changed
    >>>by the class. i want every instance to have access to the current
    >>>default settings to retrieve them or change them. so, the above line now
    >>>reads:
    >>>
    >>>static MainClass OC; // OC should be the same across all classes.

    >>
    >>This essentially changes only the class names in my response.
    >>
    >>V

    >
    >
    > It changes a lot more than that, if I understand what he's saying. He's
    > said that OC is a member of MainClass. Then he said that, instead of OC
    > being of the type OtherClass, it's actually of the type MainClass. Well,
    > that's just not possible! You can't contain a class inside itself, even if
    > it's a static member, can you? The static member, after all, would now have
    > itself as a member! Or did I miss something?


    Yes, you did miss the fact that 'OC' is a _static_ member. A static
    data member is not "contained" inside "itself". It's a very good and
    valid alternative to a global object.

    > Perhaps what is needed is just a single global object, instead of a static
    > member?


    Perhaps you shouldn't be too hasty in your posting...

    A static member that is an instance of the same class is one of relatively
    common idioms. Imagine that I need a prototype from which all instances
    are created unless some arguments are supplied to my constructor. Where
    would I keep the "default values" for all members? Hard-coded? What if
    I need to change them during run-time? I believe the OP needs exactly
    that, some kind of "prototype".

    V
     
    Victor Bazarov, Jun 9, 2005
    #7
  8. lou zion

    Howard Guest

    "Victor Bazarov" <> wrote in message
    news:x31qe.82054$01.us.to.verio.net...
    > Howard wrote:
    >> "Victor Bazarov" <> wrote in message
    >> news:qU0qe.82052$01.us.to.verio.net...
    >>
    >>>lou zion wrote:
    >>>
    >>>>>myclass.h
    >>>>>-------------------
    >>>>>class MainClass()
    >>>>>{
    >>>>> declarations...
    >>>>>
    >>>>> static OtherClass OC; // oc should be the same across all classes.
    >>>>> changes by one class instance change it in all instances
    >>>>> ...
    >>>>>}
    >>>>
    >>>>
    >>>>a new complication...
    >>>>
    >>>>OtherClass is now MainClass. the idea is this: there is a default
    >>>>setting for each variable in the class, but the defaults can be changed
    >>>>by the class. i want every instance to have access to the current
    >>>>default settings to retrieve them or change them. so, the above line now
    >>>>reads:
    >>>>
    >>>>static MainClass OC; // OC should be the same across all classes.
    >>>
    >>>This essentially changes only the class names in my response.
    >>>
    >>>V

    >>
    >>
    >> It changes a lot more than that, if I understand what he's saying. He's
    >> said that OC is a member of MainClass. Then he said that, instead of OC
    >> being of the type OtherClass, it's actually of the type MainClass. Well,
    >> that's just not possible! You can't contain a class inside itself, even
    >> if it's a static member, can you? The static member, after all, would
    >> now have itself as a member! Or did I miss something?

    >
    > Yes, you did miss the fact that 'OC' is a _static_ member. A static
    > data member is not "contained" inside "itself". It's a very good and
    > valid alternative to a global object.
    >
    >> Perhaps what is needed is just a single global object, instead of a
    >> static member?

    >
    > Perhaps you shouldn't be too hasty in your posting...


    Not hasty, just ignorant in this case, it seems....

    >
    > A static member that is an instance of the same class is one of relatively
    > common idioms. Imagine that I need a prototype from which all instances
    > are created unless some arguments are supplied to my constructor. Where
    > would I keep the "default values" for all members? Hard-coded? What if
    > I need to change them during run-time? I believe the OP needs exactly
    > that, some kind of "prototype".
    >


    Cool. I like that you can do that. I just never heard of such a thing. I
    guess it's possible because the static members aren't really stored inside
    the object, so that even that static instance itself can "refer" to the
    static instance (itself). But since OC is now a member of MainClass,
    wouldn't that allow for something like this..?

    MainClass x;
    x.OC.OC.OC.OC.OC.OC.foo(); // as many .OC as desired?

    [running off and testing...]

    Well, blow me down! That's exactly what happens! :) The following code
    works (and prints 3, as expected):

    class MC
    {
    public:
    MC(int initX = 0) : x(initX) {};

    static MC mc;
    int x;
    };

    MC MC::mc;

    int main(int argc, char* argv[])
    {
    char temp[10];

    MC a;
    a.mc.mc.mc.mc..mc.mc.mc.mc.mc.mc.x = 3;
    std::cout << a.mc.x << std::endl;
    std::cin >> temp;

    return 0;
    }


    That's gotta be the strangest code I've ever written, at least that actually
    worked! :)

    Thanks for the info, Voctor!

    -Howard
     
    Howard, Jun 9, 2005
    #8
  9. lou zion

    Howard Guest

    "Howard" <> wrote in message
    news:xj2qe.923485$w62.100715@bgtnsc05-

    > a.mc.mc.mc.mc..mc.mc.mc.mc.mc.mc.x = 3;


    fat-fingered that one. change that .. to just . and it works.
     
    Howard, Jun 9, 2005
    #9
    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. Tarlika Elisabeth Schmitz

    accessing static constant does not initialize class

    Tarlika Elisabeth Schmitz, Dec 11, 2004, in forum: Java
    Replies:
    2
    Views:
    463
    Tarlika Elisabeth Schmitz
    Dec 12, 2004
  2. RCAJ
    Replies:
    6
    Views:
    40,808
    Jeff Schwab
    Dec 28, 2003
  3. wim delvaux
    Replies:
    5
    Views:
    2,633
    Gianni Mariani
    Jun 23, 2004
  4. Nan Li
    Replies:
    7
    Views:
    560
    Nan Li
    Nov 9, 2005
  5. markww
    Replies:
    6
    Views:
    5,164
    Salt_Peter
    Aug 26, 2006
Loading...

Share This Page