Having trouble initiating an array in a structure

2019-07-12 14:07发布

问题:

class CRA_Account {
    int tax[4];
    double refund[4];
    int SIN;
 public:
    CRA_Account();
 }

 CRA_Account::CRA_Account() {
     SIN = 0;
     tax[4] = { 0 };
     refund[4] = { 0 };
 }

When I create a object in main it'll set the SIN to 0 but won't do the same to the arrays. Can someone help why?

回答1:

tax[4] = { 0 }; is wrong at many levels.. One way to initlizie your class:

CRA_Account::CRA_Account():
   tax{0,0,0,0},
   refund{0,0,0,0},
   SIN{0}{
}

Online

Try to have a look at std::array



标签: c++ class