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 >> String::String(char letter, int repeat) : name(0), stringlength(repeat) That constructor isn't quite right, but from the ...

  1. #76
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by Daved View Post
    >> String::String(char letter, int repeat) : name(0), stringlength(repeat)
    That constructor isn't quite right, but from the looks of it you added it recently and with a different style than your other ones.

    The other constructors have solved the initialization problem I mentioned before, except now one of your operator= does the same bad thing of not setting all values to valid states.

    Look at how your operator+= sets stringlength at the end of the function. It's not quite right.

    Your operator=(char) has a memory leak. Remember that you need to clean up existing memory inside operator=. Also check your setName function for the same problem.

    I assume you haven't used your operator[] functions yet. You'll see the problem when you do.


    Do you have test code? You should write a small test app that tests one function at a time. Start with a string, run one of the functions, and check the values inside the string to make sure they are accurate. do this for every function separately, and then they should work better when you combine them.
    You are right. I made the String(char letter, int repeat) constructor but I changed some of it according to the suggestions of another user and it works better now.

  2. #77
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by Daved View Post
    >> String::String(char letter, int repeat) : name(0), stringlength(repeat)
    That constructor isn't quite right, but from the looks of it you added it recently and with a different style than your other ones.

    The other constructors have solved the initialization problem I mentioned before, except now one of your operator= does the same bad thing of not setting all values to valid states.

    Look at how your operator+= sets stringlength at the end of the function. It's not quite right.

    Your operator=(char) has a memory leak. Remember that you need to clean up existing memory inside operator=. Also check your setName function for the same problem.

    I assume you haven't used your operator[] functions yet. You'll see the problem when you do.


    Do you have test code? You should write a small test app that tests one function at a time. Start with a string, run one of the functions, and check the values inside the string to make sure they are accurate. do this for every function separately, and then they should work better when you combine them.
    whats wrong with my []operators?

  3. #78
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    I'm sure you'll find out as soon as you use them. It's rather obvious.

    You should make the first one return a const char& or a char since the function is const and you shouldn't be providing non-const access to the member data. But that's not the obvious thing that is wrong with them.

  4. #79
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by Daved View Post
    I'm sure you'll find out as soon as you use them. It's rather obvious.

    You should make the first one return a const char& or a char since the function is const and you shouldn't be providing non-const access to the member data. But that's not the obvious thing that is wrong with them.
    I changed them and I think they are okay now:

    Code:
    char& String::operator [](int i) const 
    {
    	if(i >= 0 && i < stringlength)
    		return sink;
    	else
    		return buf[i];
    }
    
    char& String::operator [](int i)
    {
    	if(i >= 0 && i < stringlength)
    		return sink;
    	else
    		return buf[i];
    }

  5. #80
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    if i is -1 you will return buf[-1] - out of bounds access
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #81
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by vart View Post
    if i is -1 you will return buf[-1] - out of bounds access
    so, should it be this:

    Code:
    char& String::operator [](int i) const 
    {
    	if(i <= 0 && i < stringlength)
    		return sink;
    	else
    		return buf[i];
    }
    
    char& String::operator [](int i)
    {
    	if(i <= 0 && i < stringlength)
    		return sink;
    	else
    		return buf[i];
    }

  7. #82
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    in this case using i == stringlength will give you the out of bound access
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #83
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by vart View Post
    in this case using i == stringlength will give you the out of bound access
    I don't understand.

  9. #84
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by BKurosawa View Post
    I don't understand.
    What exactly you do not understand?

    if i is stringlength than
    i<=0 is false, the if is false and you go to the else getting buf[stringlength]
    this is out of bounds access
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  10. #85
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by vart View Post
    What exactly you do not understand?

    if i is stringlength than
    i<=0 is false, the if is false and you go to the else getting buf[stringlength]
    this is out of bounds access
    oh, so you are saying replace what I have in the if() with i == stringlength.

  11. #86
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    How do I make the print function use printf() to output the name of the string, the string literal, and the size of the string literal with the three all displayed on one line per string object?

  12. #87
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    What is string name?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  13. #88
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by vart View Post
    What is string name?
    Like if I make a string:

    String s1;

    and then I do this:

    s1.setName("s1");

    Then I should be able to show the name of the object which in this case is s1.

  14. #89
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    like
    Code:
    printf("&#37;s: %s(%d)\n", name, buf, stringlength);
    ?

    But why do you need a printf if you trying to write in C++?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  15. #90
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by vart View Post
    like
    Code:
    printf("&#37;s: %s(%d)\n", name, buf, stringlength);
    ?

    But why do you need a printf if you trying to write in C++?
    isn't printf used in C++?

Page 6 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