static class object - are builtin members 0?

S

Stephen Howe

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
 
Ö

Öö Tiib

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.
 

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

No members online now.

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top