4

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!

FOLLOW-UP: Thanks for the replies, guys. You can't do it at the declaration; that's not allowed. So the only way appears to be memset or setting the members individually. I ended up writing a utility function to do just that.

Oscar
  • 2,039
  • 2
  • 29
  • 39

4 Answers4

2

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;
egrunin
  • 24,650
  • 8
  • 50
  • 93
w.donahue
  • 10,790
  • 13
  • 56
  • 78
  • 2
    Thanks. Only the last option works in a C++ object; initializers with braces aren't allowed for members. – Oscar Nov 01 '10 at 00:20
1

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

class C {
    struct tm  _creationDate;
    struct tm  _expirationDate;
    struct tm  _lockDate;
    C() : _creationDate(), ... {}
Anycorn
  • 50,217
  • 42
  • 167
  • 261
  • Thanks. While that syntax was allowed in the initialization list, there was no way to initialize members with {0}. – Oscar Nov 01 '10 at 00:24
1

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
  • 77,323
  • 27
  • 116
  • 141
1

Only way possible in C++03:

class foo
{
    tm _creationDate;
    public:
    foo()
    {
        tm tmp_tm = {0};
        _creationDate = tmp_tm;
    }
};

Note that this will initialize _creationDate with a copy of tmp_tm, thus invoking the (most likely autogenerated) copy-constructor. So for large structs, you should rather stick with your utility function, since that will not require copying the whole struct.

And by the way, names starting with an underscore (at global scope) are reserved for the standard library implementation. Names starting with an underscore followed by an uppercase letter are reserved everywhere. Technically the name _creationDate here is fine, since this is not at global scope, but I would still recommend to avoid using a leading underscore for names.

smerlin
  • 6,446
  • 3
  • 35
  • 58
  • Names with underscore are legal - maybe you confused with *double* underscores, which are not recommended. – John Simoes Jul 20 '21 at 02:24
  • 1
    @JohnSimoes You are incorrect, but my text was also not 100% correct. I updated it accordingly. – smerlin Jul 22 '21 at 18:50
  • Thanks @smerlin, my comment above was indeed incorrect. I appreciate it and upvoted your edited answer accordingly. In the global scope variables should not start with underscores and are reserved. – John Simoes Jul 28 '21 at 00:12