If it is c_str() a la the standard C++ library, the complexity should be O(1)
To achieve this, you would need to maintain space for one extra character at the end which has a value of zero (which is not a logical part of the sequence of characters that the string holds).
The code as written would leak memory, and the call to copy doesn't seem to be right.
hello jonnin,
the class itself stores the chars in an array terminated with null. It seems that my c_str() func just only should return the pointer of the array.
This is only part of the code to see how the chars are stored:
correct just return the pointer. However I highly recommend you make an actual copy:
char* copyof = new char[strlen(data)];
strcpy(copyof, data);
return copyof;
then the user cannot change data via the pointer, which is 'bad'.
however while you class can destroy copyof pointer memory on destructor, it cant ensure the user does not have a pointer to that memory that will go bad... and that is OK but it needs to be understood and documented who owns the copyof memory and who is responsible for destroying it (the class or the user).