How to zero array members when my compiler isn't standard conform

前端 未结 3 1128
独厮守ぢ
独厮守ぢ 2021-01-19 05:29

My compiler (C++Builder6) syntactically allows array member initialization (at least with zero), but actually it doesn\'t really do it. So the assert in the example given be

相关标签:
3条回答
  • 2021-01-19 05:47

    I think you may use this:

    TT() { std::fill(b, b + 8, char()); }
    

    This way you will solve your problem while nothing is wrong with portability and standard conformance!

    0 讨论(0)
  • 2021-01-19 05:52

    You may use fill_n like suggested in: C/C++ initialization of a normal array with one default value

    If no fill_n is available, you can always use memset like:

    TT() {memset(b, 0, sizeof b);}
    
    0 讨论(0)
  • 2021-01-19 06:00

    I would like to append previous posts that if you are using a character array as a string then it is enough to write in the constructor

    TT() { b[0] = '\0'; }

    0 讨论(0)
提交回复
热议问题