static class object - are builtin members 0?

Discussion in 'C++' started by Stephen Howe, May 21, 2011.

  1. Stephen Howe

    Stephen Howe Guest

    Hi

    If I have

    #include <vector>
    #include <string>

    class MyClass
    {
    std::vector<int> a;
    std::string b[5];
    short c;
    int d[3];
    };

    void FreeFunction()
    {
    MyClass m;



    }


    then the compiler will generate a default constructor.
    The default constructor will call std::vector & std::string default
    constructors for a, b.
    But c, d members wont be initialised and could be anything for object
    m inside FreeFunction.

    Now consider that I have

    static MyClass n;

    void FreeFunction2()
    {
    static MyClass p;
    }

    Will the fact that these object are static, and for n, global as well,
    mean that c, d will be 0?

    Cheers

    Stephen Howe
     
    Stephen Howe, May 21, 2011
    #1
    1. Advertising

  2. Stephen Howe

    Öö Tiib Guest

    On May 22, 1:09 am, Stephen Howe <> wrote:
    > Hi
    >
    > If I have
    >
    > #include <vector>
    > #include <string>
    >
    > class  MyClass
    > {
    >     std::vector<int>   a;
    >     std::string           b[5];
    >     short                  c;
    >     int                      d[3];
    >
    > };
    >
    > void FreeFunction()
    > {
    >       MyClass    m;
    >
    > }
    >
    > then the compiler will generate a default constructor.
    > The default constructor will call std::vector & std::string default
    > constructors for a, b.
    > But c, d members wont be initialised and could be anything for object
    > m inside FreeFunction.
    >
    > Now consider that I have
    >
    > static  MyClass    n;
    >
    > void FreeFunction2()
    > {
    >     static  MyClass    p;
    >
    > }
    >
    > Will the fact that these object are static, and for n, global as well,
    > mean that c, d will be 0?


    There the sub-objects will be zero initialized since they are static.
    Still it makes sense to write constructor for MyClass that initializes
    the members. Sooner or later you forget it and create it dynamically
    or automatically (to stack) and so have it full of garbage.
     
    Öö Tiib, May 22, 2011
    #2
    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. SaravanaKumar
    Replies:
    6
    Views:
    9,460
    Tony Morris
    Oct 19, 2004
  2. JFCM
    Replies:
    4
    Views:
    5,759
  3. CoolPint
    Replies:
    8
    Views:
    1,002
    Jeff Schwab
    Dec 14, 2003
  4. Hicham Mouline
    Replies:
    5
    Views:
    2,411
    James Kanze
    Dec 19, 2008
  5. bdb112
    Replies:
    2
    Views:
    311
    Chris Torek
    Jul 2, 2011
Loading...

Share This Page