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