I’ve got a String class. Recently, I found the poor thing couldn’t handle:
String string = "Hello World";
I immediately decided to rectify this by adding a constructor. However, the exact parameter eludes me:
inline String(const char[] str) : Array(str, sizeof(str) / sizeof(char)) { }
generates the following error: “error C2146: syntax error : missing ‘)’ before identifier ‘str'”
What I’m thinking is that since it’s a static char * literal, it isn’t meant to be passed to a function. But, I don’t actually know.
Thanks. (To reassure – Any array passed to Array will promptly be copied, not stored)
Simplest would be to take
const char*as the parameter. Then usestrlento find the string length, then allocatelen+1characters usingnewand usestrncpyto copy the string to the newly allocated memory. BTW, any specific reason not to usestd::string?