C++ Array Initializers Warnings

后端 未结 2 479
暗喜
暗喜 2021-01-14 14:59

I have declared and initialized a constant char array within a class:

class grid {
    const char test[11] = {\'s\', \'e\', \'1\', \'2\', \'3\', \'4\', \'5\'         


        
相关标签:
2条回答
  • 2021-01-14 15:28

    You need do compile with -std=c++11 (for gcc and clang). If you do not do this, your program is checked vs. the old C++98 standard to ensure compatibility with old compilers.

    So it allows you to use C++11 features without the flag, but warns you so that you don't do it on accident.

    Further explanation:

    Your code compiles fine because it is legal code and the compiler can compile it. The compiler omits a warning to make you aware of the fact that you used a C++11 feature because many people (like my University, sadly) still use outdated compiler like gcc4.6 that do not have full C++11 support yet. That means that these people might not be able to compile your code, which you might care about (if e.g. your professor needs to compile your assignment).

    With the -std=c++11 flag you tell the compiler "This is a C++11 program, meant to be compiled with C++11 compliant compilers". Thus, the warning becomes redundant.

    0 讨论(0)
  • 2021-01-14 15:36

    To make it C++98 compatible, you need to initialize non-static class constants outside of the class declaration.

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