Constructor to specify zero-initialization of all builtin members?

前端 未结 3 1545
庸人自扰
庸人自扰 2020-12-06 07:53

Is there a simpler way for a class\'s constructor to specify that all members of built-in type should be zero-initialized?

This code snippet came up in another pos

3条回答
  •  爱一瞬间的悲伤
    2020-12-06 08:56

    For something working in C++98, you can use Boost's value_initialized: (live example)

    #include 
    ...
    struct Money
    {
        boost::value_initialized amountP, amountG, totalChange;
        boost::value_initialized twenty, ten, five, one, change;
        boost::value_initialized quarter, dime, nickel, penny;
    
        void foo();
        Money()  {/*every member is 0*/}
    };
    

提交回复
热议问题