String Class

This is a discussion on String Class within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by BKurosawa Can you look at the code below to see if this is correct? Code: String& String::operator ...

  1. #31
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by BKurosawa View Post
    Can you look at the code below to see if this is correct?

    Code:
    String& String::operator ++()
    {
    	buf[stringlength] = 'X';
    	stringlength++;
    	temp = new char[stringlength];
    	strcpy(temp, buf);
    	return *this;
    }
    I suggest that you allocate the bigger buffer first, then copy the string, then set the last char to "X".

    And it's definitely not doing what you described (but for some reason I quoted DaveD's post rahter than the original poster):
    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?
    Your code adds one to the LENGTH of the string, and appends an "X" on the end of the string - or at least it looks like that's what you were trying to do, but did things a little bit wrong order - I can't see how that is of any use to anyone (not that adding/subtracting one from each character is particularly useful either, unless you are using it for very basic encryption).

    --
    Mats

  2. #32
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by Daved View Post
    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.
    Then what do I need to do to fix it

  3. #33
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Do you have any ideas?

    The problem is that by adding the 'X' at the beginning, you are corrupting the string so that it doesn't copy correctly. Does that help give you a clue as to a solution?

  4. #34
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by Daved View Post
    Do you have any ideas?

    The problem is that by adding the 'X' at the beginning, you are corrupting the string so that it doesn't copy correctly. Does that help give you a clue as to a solution?
    What needs to be put there instead of the 'X'? earlier you said that the 'X' is bad because it is replacing the null character.

  5. #35
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need to consider the order you are doing things in. Inserting an X is fine, as long as you are not destroying anything valuable. In your implementation you are writing X over the "nul" that is ending the string, which can lead to ALL sorts of trouble.

    Also, if you insert an X, you need to make sure the NEXT char is a NUL.

    --
    Mats

  6. #36
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by matsp View Post
    You need to consider the order you are doing things in. Inserting an X is fine, as long as you are not destroying anything valuable. In your implementation you are writing X over the "nul" that is ending the string, which can lead to ALL sorts of trouble.

    Also, if you insert an X, you need to make sure the NEXT char is a NUL.

    --
    Mats
    Can I completely delete the first line in the code?

  7. #37
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, that wouldn't really work that well either, would it. The stringlength is now longer, but the ACTUAL string is one shorter than stringlen says, which is probably not what someone wanted if they called this function expecting the string to grow (now, WHY anyone would want a string to expand with an X is a different story).

    --
    Mats

  8. #38
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by matsp View Post
    Well, that wouldn't really work that well either, would it. The stringlength is now longer, but the ACTUAL string is one shorter than stringlen says, which is probably not what someone wanted if they called this function expecting the string to grow (now, WHY anyone would want a string to expand with an X is a different story).

    --
    Mats
    I guess I do not want to expand the string with an X. All I want to do is to have the value 1 be added to each character in the string literal pointed at by buf

  9. #39
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,450
    I guess I do not want to expand the string with an X. All I want to do is to have the value 1 be added to each character in the string literal pointed at by buf
    Loop over the characters and add 1 to each character. Still, this sounds like an abuse of operator++ as a named free function would be more descriptive (what does it mean to add 1 to a string? Oh, you actually want to add 1 to each character). Why must you do it this way, anyway? Homework from a teacher who does not know about good class interface design?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #40
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by laserlight View Post
    Loop over the characters and add 1 to each character. Still, this sounds like an abuse of operator++ as a named free function would be more descriptive (what does it mean to add 1 to a string? Oh, you actually want to add 1 to each character). Why must you do it this way, anyway? Homework from a teacher who does not know about good class interface design?
    I am unsure as to how to implement 1 being added to each character.

  11. #41
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,450
    I am unsure as to how to implement 1 being added to each character.
    What part are you unsure of? The loop, adding 1, or both?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #42
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by laserlight View Post
    What part are you unsure of? The loop, adding 1, or both?
    Well, I don't think I should be doing this: stringlength++. Should I be? Do I just make a loop that goes through every single character of buf and add one to it? If that is the case, then how do I add one to each character?

  13. #43
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,450
    Well, I don't think I should be doing this: stringlength++. Should I be?
    That adds one to the string length. You do not want to change the string length, you want to change each individual character in the string.

    Do I just make a loop that goes through every single character of buf and add one to it?
    Yes.

    If that is the case, then how do I add one to each character?
    You can increment the first character with ++buf[0]. I'll leave you to figure out the rest
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #44
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by laserlight View Post
    That adds one to the string length. You do not want to change the string length, you want to change each individual character in the string.


    Yes.


    You can increment the first character with ++buf[0]. I'll leave you to figure out the rest
    should i just scrap my entire code for that operator and start over?

  15. #45
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,450
    should i just scrap my entire code for that operator and start over?
    Looks like it.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 3 of 8 FirstFirst 12345678 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. class object manipulation
    By guda in forum C++ Programming
    Replies: 2
    Last Post: 10-09-2004, 10:43 AM
  5. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21