initialize array size from another array value

后端 未结 3 1466
难免孤独
难免孤独 2020-12-18 09:48
#include 
using namespace std; 

const int vals[] = {0, 1, 2, 3, 4}; 

int newArray[ vals[2] ]; //\"error: array bound is not an integer constant\"

         


        
3条回答
  •  一整个雨季
    2020-12-18 10:06

    It's possible that the value of a const expression is not even known at compile time. For example, you can initialize a constant with something returned from a function, like

    const int size = rand(); // random size
    

    So it is not that constant as you might think

提交回复
热议问题