Initializing struct

I have a struct

struct Date {
int day;
int month;
int year;
} ;

I instantiate an array of Date

Date* dates = new Date[length];

How do I initialize all members of the Date array in the dates array to '0' easily?
By using a constructor, like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
struct Date
{
    int day;
    int month;
    int year;

    Date()
    {
        day=0;
        month=0;
        year=0;
    }
};

or like this:

1
2
3
4
5
6
7
8
9
10
struct Date
{
    int day;
    int month;
    int year;

    Date():day(0),
           month(0),
           year(0){}
};
Last edited on
1
2
3
4
5
6
7
8
9
10
struct Date
{
    int day;
    int month;
    int year;

    Date(int d=0, int m=0, int y=0):day(d),
           month(m),
           year(y){}
};

this give you more options and flexibility.
Last edited on
For POD types - there is also the lesser known:

Date* dates = new Date[6]();
Probably the best approach is

1
2
3
4
struct Date {
    Date() : day(), month(), year() {}
    Date( int d, int m, int y ) : day( d ), month( m ), year( y ) {}
};


or, if you decide to use everid's approach (one that I've shied away from more and more
lately), you should declare the constructor explicit.
or, if you decide to use everid's approach (one that I've shied away from more and more
lately), you should declare the constructor explicit.


Do you care to expand on why you've shied away from that lately?

I see why the approach given by guestgulkan is fairly opaque to what is happening, and could confuse a novice, but what more could be explicit in everid's approach?
When you have a constructor that only takes one argument, if you don't declare it with the explicit keyword, the compiler will automatically assume you can convert values like this:

Date d = 4;

which is not what any user of Date would be looking for. Use of the explicit keyword prevents that kind of conversion.
The advantage of writing a useful constructor over, say, this:

1
2
3
4
Date d;
d.day = 4;
d.month = 2;
d.year = 42;


is that if you ever wanted to change Date to include something else, say a day of the week, you have
to find all places in the code where you instantiate a Date and fix them. Finding all the places can be
error prone -- it is very easy to miss one, and you'll get no complaint from the compiler, just that
instance isn't fully initialized.

I'd like to make it 100% fool proof. The problem with everid's approach is that it naturally wants me
to make the update by adding a fourth parameter to the constructor and defaulting it too, which means
that my code:

 
Date d( 4, 2, 42 );


compiles without complaint even though the day of the week is now set to whatever the parameter defaults
to, and I'm back to my original problem.

With my approach, I add a new parameter, and now every place where I instantiate a Date with only three
parameters instead of four is a compile error -- the compile finds all the places in the code I need to update
for me, 100% foolproof. I'm now much less likely to accidentally introduce a bug due to an uninitialized member.
Also, is

Date d(31);

valid? Can you really call it a 'date' if you don't know the month and year?

It's also not out of the realm of possibility for someone to write

Date d(20100601);

which would definitely not have the desired effect, but would compile without complaint given the 3 default parameter implementation.

--Rollie
Topic archived. No new replies allowed.