Ex: Is this acceptable? It compiles and seems to work for me; so is this bad form?
.h file
class MyClass
{
static char c[];
};
.cpp file
char MyClass::c[] = "abcde";
Or must I do this instead, for example?
.h file
class MyClass
{
static char c[10];
};
.cpp file
char MyClass::c[10] = "abcde";
Is there a benefit to one technique over the other? I'm not sure if I'm missing something. I don't know what I don't know, ya know?
Update:
The original code I posted looked like this below. I edited it to make it as shown above since I didn't mean for the "private" aspect of it to be the point of discussion. In my real code (running on an Arduino), I am using .h and .cpp files and the static member is only intended to be accessed by the class. I guess I'm learning something new though too, as the answers regarding the below code seem to tell me that private static members are the same as public static members ie: they can both be modified by anything outside the class if static. That, I didn't know. WRONG, see the answer by Alok Save here. More on static member variables here. This line was especially helpful to me: "Because static member variables are not part of the individual objects, you must explicitly define the static member if you want to initialize it to a non-zero value...This initializer should be placed in the code file for the class (eg. Something.cpp). In the absense of an initializing line, C++ will initialize the value to 0."
class MyClass
{
private:
static char c[];
};
char MyClass::c[] = "abcde";
Or must I do this instead, for example?
class MyClass
{
private:
static char c[10];
};
char MyClass::c[10] = "abcde";