Now maybe I just suck at C++, and that's probably true, but it seems completely backwards and stupid to allocate an array of an unknown size and then mess with the first two bytes.
Hint: Allocate the 2 bytes you're using.
'\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.
/me bashes keyboard into monitor while yelling, "Two! Two, I said! Two!"
Make stringlength a size_t variable instead of 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.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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Okay, Can anybody else make suggestions on my +=, ++, or + operators? Are there any errors?
Frankly, I would remove the operator++ and operator--, they do not typically make sense where strings are concerned.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
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.
The report_error function would do something like this:Code:... // Test concatenation with += String s1, s2; s1 = "abc"; s2 = "def"; s1 += s2; if (s1 != "abcdef") report_error(String("operator+="), s1, String("abcdef")); ...
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.Code:void report_error(String func, String res, String expected) { cout << "*** Error ***" << "Failed function " << function << " expected " << expected << " got " << res << endl; errcnt++; }
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.
--
Mats
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.