I am originally a Java programmer and I have a deep love of the syntax, especially regarding the String object. With C++, I have tried to recreate the toUpperCase() method that Java has. The only problem is that it always returns a String object that has an empty/NULL char array.
String String::toUpperCase() { char *a = new char[this->length + 1]; memset(a, 0, this->capacity + 1); memcpy(a, this->characters, this->length); for (int i = 0; i < strlen(this->characters); i++) { toupper(a[i]); } return *new String(a); }
std::stringin general. This function leaks memory every time it's called. Also, read some documentation fortoupper.newa lot, in C++ you should avoid it when possible (no GC).return String(a);instead of 'return *new String(a);' and use a smart pointer ona, or better yet, astd::vector); Also, stop writingthis->. It is unnecessary. Also consider usingstd::fillinstead ofmemsetandstd::copyinstead ofmemcpy.