Are members of a c++ class initialized to 0 by the default constructor ?

Discussion in 'C++' started by Skybuck Flying, Jun 21, 2011.

  1. Hello,

    My question is basically:

    Are members of a c++ class initialized to 0 by the default constructor ?

    For example:

    class TSomeClass
    {
    private:
    int mField1;
    int mField2;

    protected:

    public:
    TSomeClass();
    ~TSomeClass();
    };

    // constructor
    TSomeClass::TSomeClass()
    {

    }

    // destructor
    TSomeClass::~TSomeClass()
    {

    }

    Usage example:

    class TSomeOtherClass
    {
    private:
    TSomeClass mSomeClass();

    public:
    // default constructor here etc.


    }

    int main()
    {
    TSomeClass vSomeClass();

    TSomeClass *vSomeClassArray;

    vSomeClassArray = new TSomeClass[100];

    return 0;
    }


    So will all class objects members be initialized to zero ?

    So for example vSomeClass, mSomeClass and vSomeClassArray ?

    Or perhaps the last one needs to be:

    vSomeClassArray = new TSomeClass[100]();

    ^ To call default constructor for each instance or does that happen
    automatically ?

    (I guess it's safest to initialize all members in the constructors, but I'd
    like to get my code up and running fast, so having to initialize them all
    manually to zero is a bit of a drag/boring... but I will probably do so
    anyway later just to make sure... but on the other hand maybe not... since
    this code is supposed to be pretty high performant ;))

    Bye,
    Skybuck.
    Skybuck Flying, Jun 21, 2011
    #1
    1. Advertising

  2. Re: Are members of a c++ class initialized to 0 by the default constructor?

    On 6/21/2011 10:12 AM, Skybuck Flying wrote:
    > Hello,
    >
    > My question is basically:
    >
    > Are members of a c++ class initialized to 0 by the default constructor ?


    Some are.

    >
    > For example:
    >
    > class TSomeClass
    > {
    > private:
    > int mField1;
    > int mField2;
    >
    > protected:
    >
    > public:
    > TSomeClass();
    > ~TSomeClass();
    > };
    >
    > // constructor
    > TSomeClass::TSomeClass()


    In this case, since you omitted the 'mField' members from the (now
    empty) initializer list, they are *uninitialized*.

    > {
    >
    > }
    >
    > // destructor
    > TSomeClass::~TSomeClass()
    > {
    >
    > }
    >
    > Usage example:
    >
    > class TSomeOtherClass
    > {
    > private:
    > TSomeClass mSomeClass();


    That's a declaration of a function.

    >
    > public:
    > // default constructor here etc.
    >
    >
    > }
    >
    > int main()
    > {
    > TSomeClass vSomeClass();


    That's a declaration of a function. See FAQ section 10.

    >
    > TSomeClass *vSomeClassArray;
    >
    > vSomeClassArray = new TSomeClass[100];
    >
    > return 0;
    > }
    >
    >
    > So will all class objects members be initialized to zero ?


    No.

    >
    > So for example vSomeClass, mSomeClass and vSomeClassArray ?


    'vSomeClass' is a function.

    'mSomeClass' is a function.

    All elements of the array, to the beginning of which 'vSomeClassArray'
    points are default-initialized.

    >
    > Or perhaps the last one needs to be:
    >
    > vSomeClassArray = new TSomeClass[100]();
    >
    > ^ To call default constructor for each instance or does that happen
    > automatically ?


    There is no need. Since 'TSomeClass' is presumably a class, the expressions

    new TSomeClass[100]

    and

    new TSomeClass[100]()

    are equivalent. If 'TSomeClass' is a typedef-id that is defined as a
    built-in type, for instance 'int*', then the former expression leaves
    the elements of the array *uninitialized*, whereas the latter
    zero-initializes them.

    > (I guess it's safest to initialize all members in the constructors, but
    > I'd like to get my code up and running fast, so having to initialize
    > them all manually to zero is a bit of a drag/boring... but I will
    > probably do so anyway later just to make sure... but on the other hand
    > maybe not... since this code is supposed to be pretty high performant ;))


    I think you need to educate yourself on optimizing performance and
    micro-optimizations and what the difference is.

    >
    > Bye,
    > Skybuck.


    T
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jun 21, 2011
    #2
    1. Advertising

  3. Re: Are members of a c++ class initialized to 0 by the default constructor?

    * Skybuck Flying, on 21.06.2011 16:12:
    >
    > My question is basically:
    >
    > Are members of a c++ class initialized to 0 by the default constructor ?


    Nope.

    Unless you define one that does that.

    But for a Plain Old Data type (which does not have a user-defined constructor)
    you can ask for zero-initialization where you create an object, like

    p = new T(); // The parenthesis asks for value-initialization,
    // which for a Plain Old Data type is zeroing.


    > For example:
    >
    > class TSomeClass
    > {
    > private:
    > int mField1;
    > int mField2;
    >
    > protected:
    >
    > public:
    > TSomeClass();
    > ~TSomeClass();
    > };
    >
    > // constructor
    > TSomeClass::TSomeClass()
    > {
    >
    > }


    This constructor does not zero-initialize the members.

    But you could ask for it, like

    TsomeClass::TSomeClass()
    : mField1()
    , mField2()
    {}


    >
    > // destructor
    > TSomeClass::~TSomeClass()
    > {
    >
    > }
    >
    > Usage example:
    >
    > class TSomeOtherClass
    > {
    > private:
    > TSomeClass mSomeClass();
    >
    > public:
    > // default constructor here etc.
    >
    >
    > }
    >
    > int main()
    > {
    > TSomeClass vSomeClass();


    Note that this is a function declaration, not a variable declaration.


    > TSomeClass *vSomeClassArray;
    >
    > vSomeClassArray = new TSomeClass[100];
    >
    > return 0;
    > }
    >
    >
    > So will all class objects members be initialized to zero ?


    Not guaranteed with your code, no. But the compiler may emit code to do it anyway.

    I think you could ask for it like

    vSomeClassArray = new TSomeClass[100]();

    but I'm not sure of the exact syntax here. Nor about whether it is supported by
    Microsoft's compiler.


    [snip]

    The thing you need to keep clear in your mind is that for POD type, there is not
    really any constructor. Then the initialization performed depends on the
    instantiation expression. T versus T().


    Cheers & hth.,

    - Alf

    --
    blog at <url: http://alfps.wordpress.com>
    Alf P. Steinbach /Usenet, Jun 21, 2011
    #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. CoolPint
    Replies:
    8
    Views:
    957
    Jeff Schwab
    Dec 14, 2003
  2. Steven T. Hatton
    Replies:
    4
    Views:
    399
    Siemel Naran
    Aug 27, 2004
  3. Generic Usenet Account
    Replies:
    10
    Views:
    2,202
  4. Olumide
    Replies:
    4
    Views:
    528
    James Kanze
    Dec 9, 2007
  5. abhash
    Replies:
    4
    Views:
    420
    dizzy
    Jun 12, 2008
Loading...

Share This Page