static data members

Discussion in 'C++' started by subramanian100in@yahoo.com, India, Jan 8, 2010.

  1. , India

    , India Guest

    In the book C++ Primer(Fourth Edition) by Stanley Lippman, the
    following is mentioned in page 469 section '12.6.2 static Data
    members':
    Unlike ordinary data members, static members are not initialized
    through class constructor(s) and instead should be initialized when
    they are defined.

    I do not understand this sentence. Kinldy explain what it means with
    example.

    Thanks
    V.Subramanian
    , India, Jan 8, 2010
    #1
    1. Advertising

  2. , India

    Tim Love Guest

    ", India" <> writes:

    >In the book C++ Primer(Fourth Edition) by Stanley Lippman, the
    >following is mentioned in page 469 section '12.6.2 static Data
    >members':
    >Unlike ordinary data members, static members are not initialized
    >through class constructor(s) and instead should be initialized when
    >they are defined.


    >I do not understand this sentence. Kinldy explain what it means with
    >example.


    I think it means that this works

    class a {
    public:
    int i;

    a(){ i=7;};

    };

    int main() {
    a x;
    }


    but this doesn't

    class a {
    public:
    static int i;

    a(){ i=7;};

    };

    int main() {
    a x;
    }



    However, this does


    class a {
    public:
    static int i;
    };

    int a::i=7;

    int main() {
    a x;
    }
    Tim Love, Jan 8, 2010
    #2
    1. Advertising

  3. On Jan 8, 4:35 pm, ", India"
    <> wrote:
    > In the book C++ Primer(Fourth Edition) by Stanley Lippman, the
    > following is mentioned in page 469 section '12.6.2 static Data
    > members':
    > Unlike ordinary data members, static members are not initialized
    > through class constructor(s) and instead should be initialized when
    > they are defined.
    >
    > I do not understand this sentence. Kindly explain what it means with
    > example.


    Suppose we have this class definition:

    class X
    {
    public:
    X();

    private:
    int m_a;
    static int m_b;
    };

    Now each new instance of type X, like:

    X x;

    Will be initialised by X's constructor like this:

    X::X() : m_a(12)
    {
    }

    So, each new instance of X has its own instance of its member
    variables
    that can be initialised when the object is first created by the class'
    constructor.

    m_b on the other hand is a static member variable. static members
    belong
    to the class rather than to its instances. There is always a single
    instance of a static member, whether there are zero, one or many
    instances
    of the class. This means that a static member can only be initialised
    once.
    That is not in the per instance constructor, but like this:

    int X::m_b = 100;

    This initialisation is performed once before the entry of main().
    Gert-Jan de Vos, Jan 8, 2010
    #3
  4. , India

    SG Guest

    , India wrote:
    > In the book C++ Primer(Fourth Edition) by Stanley Lippman, the
    > following is mentioned in page 469 section '12.6.2 static Data
    > members':
    > Unlike ordinary data members, static members are not initialized
    > through class constructor(s) and instead should be initialized when
    > they are defined.
    >
    > I do not understand this sentence. Kinldy explain what it means with
    > example.


    "when they are defined" is not the same as "when they are declared".
    Is it possible that you confuse these two?

    class foo {
    public:
    static int j; // NOT a definition, just a declaration
    };

    ...

    int foo::j; // Definition (allocates memory in TU)

    C++ allows you to use an initializer as well. You can replace the last
    line with

    int foo::j = 23;


    Cheers,
    SG
    SG, Jan 8, 2010
    #4
  5. , India

    Jerry Coffin Guest

    In article <6b689832-abcc-459d-9744-10f9371a0330
    @k19g2000yqc.googlegroups.com>, says...
    >
    > In the book C++ Primer(Fourth Edition) by Stanley Lippman, the
    > following is mentioned in page 469 section '12.6.2 static Data
    > members':
    > Unlike ordinary data members, static members are not initialized
    > through class constructor(s) and instead should be initialized when
    > they are defined.
    >
    > I do not understand this sentence. Kinldy explain what it means with
    > example.


    A normal data member is initialized by the ctor for the class. A
    static data member is basically a global variable with a strange name
    -- the name of the class, a double colon, then the name of the
    variable itself. Despite the oddity of the name, it still has to be
    defined at global scope just like any other global variable:

    class XXX {
    // this defines the data member xxx
    int xxx;

    // this only _declares_ the static member yyy
    static int yyy;

    public:
    // Here we initialize xxx
    XXX() : xxx(123) {}
    };

    // Here we define XXX::yyy, and initialize it with the value 456
    int XXX::yyy = 456;

    --
    Later,
    Jerry.
    Jerry Coffin, Jan 8, 2010
    #5
    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:
    416
  2. SaravanaKumar
    Replies:
    6
    Views:
    9,395
    Tony Morris
    Oct 19, 2004
  3. JFCM
    Replies:
    4
    Views:
    5,734
  4. Rakesh Sinha
    Replies:
    4
    Views:
    1,848
    Rakesh Sinha
    Jan 13, 2005
  5. Hicham Mouline
    Replies:
    5
    Views:
    2,366
    James Kanze
    Dec 19, 2008
Loading...

Share This Page