views:

119

answers:

5

Hi all. I've looked all over the place, but haven't found an answer to this.

I have a C++ class with these protected members:

 struct tm  _creationDate;
 struct tm  _expirationDate;
 struct tm  _lockDate;

I want to initialize them at instantiation time. If I put this in the constructor:

 _creationDate = {0};
 _expirationDate = {0};
 _lockDate = {0};

the compiler complains: "expected primary-expression before '{' token"

I also can't find a way to do it in a member-initializer list at the top of the constructor. How does one do this? Thanks!

+1  A: 

http://www.cprogramming.com/tutorial/initialization-lists-c++.html

class C {
    struct tm  _creationDate;
    struct tm  _expirationDate;
    struct tm  _lockDate;
    C() : _creationDate(), ... {}
aaa
+1  A: 

You can either do them at declaration like this

_creationDate = {0}; // set everything to 0's

or like this

_creationDate = { StructElement1, StructElement2, ..., StructElement n);

such as

_creationDate = { 0, false, 44.2);

Or in your constructor just call out each element in the structure and initialize such as...

_creationData.thing1 = 0;
_creationData.thing2 = false;
_creationData.thing3 = 66.3;
metalideath
A: 

I think you can only initialize structure like that when you define it:

struct tm a = {0}; // works ok
struct tm b;
b = {0};           // error

One option is either to use "default" values

class a
{
    a() : t() {}
    struct tm t;
};

Or memset in constructor:

struct tm creationDate;
memset((void*)&creationDate, 0, sizeof(struct tm));
stefanB