-
January 21st, 2011, 05:07 PM
#12
Re: Class design
Originally Posted by GCDEF
Just change the return type to MyString&
Your double pointer code should work. Did you try it?
Tried it, didn't work. Can you please write the code lines so I'll find out if I did it the way you meant?
Originally Posted by GCDEF
One other thought. Your operator [] should probably just return a char, not a MyString and it should check that the int passed in is valid.
Also, you're creating a temporary MyString, but not assigning it a value, then attempting to access elements of s, which is meaningless since it has no value yet.
Changed, thanks.
Originally Posted by Lindley
Your present approach of using a fixed-size char array is better than what you had in the first post, but it's not ideal. You haven't done any checks for whether the passed string will fit in the array you have available, and your object will be much larger than necessary for small strings.
What you really want to do is dynamically allocate the memory necessary to copy the passed in string. However, doing this brings along several additional responsibilities:
1) You must release the allocated memory in the destructor.
2) You must ensure you allocate a different block of memory when copying, not just copy the pointer. (Unless you want to do reference counting, but that's even more difficult.)
3) If your string is reassigned to be longer, you must modify your allocation safely to be able to handle the new value.
Thank you, I will make those changes as soon as my program runs on a simple input.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|