Monday, August 20, 2012

Initialize for struct members in C++

How to initialize for struct member as bellow

struct ExSt{
    std:string str;
    int a;
    int b;
} ss_;

Normally, to initialize for ss_ we may use memset (&ss_, 0, sizeof (struct ExSt)). But in this case, it cause hang, because of 'str' is a class.

A solution to initialize for ss_ is:

Define constructor for struct ExSt, in constructor, we initialize for its members.

struct ExSt{
    ExSt():a(0), b(0)
    {};
    std:string str;
    int a;
    int b;

} ss_;

No comments:

Post a Comment