For classes with static members

Discussion in 'C++' started by parag_paul@hotmail.com, Dec 18, 2007.

  1. Guest

    Can a class with out any instances present have it static members
    updated.

    If yes, on which object does the update happen.
    -Parag
    , Dec 18, 2007
    #1
    1. Advertising

  2. Kira Yamato Guest

    On 2007-12-18 15:00:56 -0500, ""
    <> said:

    > Can a class with out any instances present have it static members
    > updated.


    Yes.

    >
    > If yes, on which object does the update happen.


    None. Static member variables belong to the class, not to any object.

    --

    -kira
    Kira Yamato, Dec 18, 2007
    #2
    1. Advertising

  3. Rahul Guest

    On Dec 19, 1:00 am, "" <>
    wrote:
    > Can a class with out any instances present have it static members
    > updated.
    >
    > If yes, on which object does the update happen.
    > -Parag


    But yes one needs to make sure that they are initialized once before
    any usage.
    The initialization makes sure that the memory is allocated for the
    static member variables.
    Any usage of static member variables without initialization could
    cause unresolved external symbol linker error...
    Rahul, Dec 19, 2007
    #3
  4. James Kanze Guest

    On Dec 19, 4:45 am, Rahul <> wrote:
    > On Dec 19, 1:00 am, ""
    > <> wrote:


    > > Can a class with out any instances present have it static
    > > members updated.


    > > If yes, on which object does the update happen.


    > But yes one needs to make sure that they are initialized once
    > before any usage.


    That's true for most variables, but not for static POD types,
    which are automatically zero initialized before any code is run.

    > The initialization makes sure that the memory is allocated for
    > the static member variables.


    The initialization of a static variable has nothing to do with
    memory allocation.

    > Any usage of static member variables without initialization
    > could cause unresolved external symbol linker error...


    You'll likely get an unresolved external symbol error from the
    linker if you fail to define the symbol, but the linker doesn't
    care in the least about initialization.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Dec 19, 2007
    #4
  5. Rahul Guest

    On Dec 19, 3:00 pm, James Kanze <> wrote:
    > On Dec 19, 4:45 am, Rahul <> wrote:
    >
    > > On Dec 19, 1:00 am, ""
    > > <> wrote:
    > > > Can a class with out any instances present have it static
    > > > members updated.
    > > > If yes, on which object does the update happen.

    > > But yes one needs to make sure that they are initialized once
    > > before any usage.

    >
    > That's true for most variables, but not for static POD types,
    > which are automatically zero initialized before any code is run.
    >
    > > The initialization makes sure that the memory is allocated for
    > > the static member variables.

    >
    > The initialization of a static variable has nothing to do with
    > memory allocation.



    What is the initialization meant for?
    >
    > > Any usage of static member variables without initialization
    > > could cause unresolved external symbol linker error...

    >
    > You'll likely get an unresolved external symbol error from the
    > linker if you fail to define the symbol, but the linker doesn't
    > care in the least about initialization.
    >
    > --
    > James Kanze (GABI Software) email:
    > Conseils en informatique orientée objet/
    > Beratung in objektorientierter Datenverarbeitung
    > 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    Rahul, Dec 19, 2007
    #5
  6. Ian Collins Guest

    Rahul wrote:
    > On Dec 19, 3:00 pm, James Kanze <> wrote:
    >> On Dec 19, 4:45 am, Rahul <> wrote:
    >>
    >>> On Dec 19, 1:00 am, ""
    >>> <> wrote:
    >>>> Can a class with out any instances present have it static
    >>>> members updated.
    >>>> If yes, on which object does the update happen.
    >>> But yes one needs to make sure that they are initialized once
    >>> before any usage.

    >> That's true for most variables, but not for static POD types,
    >> which are automatically zero initialized before any code is run.
    >>
    >>> The initialization makes sure that the memory is allocated for
    >>> the static member variables.

    >> The initialization of a static variable has nothing to do with
    >> memory allocation.

    >
    >
    > What is the initialization meant for?


    To initialise the state of the variable, just like any other. Leaving a
    static member uninitialised doesn't mean it's not there.

    --
    Ian Collins.
    Ian Collins, Dec 19, 2007
    #6
    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. Ben
    Replies:
    3
    Views:
    411
  2. maxw_cc
    Replies:
    6
    Views:
    509
    James Brady
    Dec 4, 2003
  3. JFCM
    Replies:
    4
    Views:
    5,719
  4. Daz
    Replies:
    5
    Views:
    567
  5. Rit
    Replies:
    23
    Views:
    918
    Mike Schilling
    Jan 3, 2010
Loading...

Share This Page