String Class

This is a discussion on String Class within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by Daved
Do you mean String(char*)? If so, I'm not sure you need it. The String(const char*) should ...

Do you mean String(char*)? If so, I'm not sure you need it. The String(const char*) should work for both char* and const char*. If you really do mean String(char), then I'm not sure if you need that either. Do you really want to initialize your string with a single character? If you do, then treat it like a string with one character but instead of using strcpy just assign that character to the first index in the array you allocate and add the null as the second.

'\0' is the null character, which is what I was referring to when I said null. I think NULL would probably end up working in C++, but I would stick with '\0' instead. That is of course in addition to MacGyver's hint.

if I make stringlength a size_t variable then it screws the rest of my code up. I have to have stringlength be an int.

Change the rest of your code. If you do not want to be necessarily stuck with std::size_t, you could use a typedef, say size_type, such that the member function getLength() returns a String::size_type instead of an int, and this would also be the type of stringlength.

I have to have it. The ++ operators are supposed to add one to each character in the string literal pointed at by buf. The --operators subtract one from each character.
Does anybody seen any errors or have any other suggestions?

>> Can anybody else make suggestions on my +=, ++, or + operators? Are there any errors?
Yes, there are errors. Your operator++ functions add to the size of the string, but you don't increase the size of your array. You need to reallocate like you do in oeprator+=.

Also, I would imagine that you would want operator+ functions that take a const char* as one of the parameters so you can add a string literal or C style string to your class.

>> Can anybody else make suggestions on my +=, ++, or + operators? Are there any errors?
Yes, there are errors. Your operator++ functions add to the size of the string, but you don't increase the size of your array. You need to reallocate like you do in oeprator+=.

Also, I would imagine that you would want operator+ functions that take a const char* as one of the parameters so you can add a string literal or C style string to your class.

Although this is not even remotely what the code you posted earlier actually DOES to the string...

Have you thought about writing some code that verifies the functionality, e.g.

I'd suggest that you do several testcases for each operator and basic function (such as constructors), and make sure that each does what you want.

It is part of good programming practice to write tests for the module or functions that you produce, and it's a good habit to do proper testing even when it's a "hobby project". You will get much better reputation amongst your collegues/customers/friends if you test your code properly before committing it to be used by others.

>> Can anybody else make suggestions on my +=, ++, or + operators? Are there any errors?
Yes, there are errors. Your operator++ functions add to the size of the string, but you don't increase the size of your array. You need to reallocate like you do in oeprator+=.

Also, I would imagine that you would want operator+ functions that take a const char* as one of the parameters so you can add a string literal or C style string to your class.

That's the idea, but it's not quite correct yet. You are replacing the terminating null character with 'X', strcpy needs the terminating null to know how many characters to copy, so by replacing the null character you will confuse strcpy and you will read and write past the bounds of your arrays.