I can’t seem to wrap my head around the reason the following code won’t compile.
In my header file I declared an array as a static class member:
class foo {
private:
#define SIZE 50
static char array[SIZE];
// further code goes here
}
In the implementation, I have to initialize the array.
char foo::array[SIZE] = new[] char[SIZE];
This yields me an error everytime – the compiler says:
cannot convert from ‘char *’ to ‘char [50]’
Why does the compiler interpret new[] char[SIZE] as char*?
new []. You need to remove the[].::operator newreturns a pointer, not an array. Pointers are not arrays, and trying to treat the two interchangeably will result in pain. Just because arrays will decay into pointers doesn’t mean that arrays are pointers.foo::array[SIZE]is alreadystatic— there’s no need to allocate storage for it in any case.statics are; if the array was just written that way withoutstaticin a function then it would be allocated on the stack.