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

S

Skybuck Flying

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.
 
V

Victor Bazarov

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
 
A

Alf P. Steinbach /Usenet

* 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top