Thread: String Class

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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];
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    if i is -1 you will return buf[-1] - out of bounds access
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    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];
    }

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    in this case using i == stringlength will give you the out of bound access
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    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.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    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.

  8. #8
    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?

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    What is string name?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    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.

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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++?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    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++?

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Going back to your operator[], you fixed the wrong thing. I'm guessing you tried it and an exception was thrown? You have to try to understand why that happens. When I said it was obvious, I didn't mean it would necessarily be obvious when you first look at it (otherwise you wouldn't have coded it that way), I just meant that once you realized what it was it would make sense. It sounds like you still don't know what the flaw was.

    So your fix was to return sink instead of throwing an exception in the first if statement. That wasn't where the problem was. The problem was the if. Pick a number and run it through the if and see where you get. You should always do this in your mind or on paper to test the if to see if it does what you want. Let's say the stringlength is 8 and the user wants the character at position 3. Follow your if in the earlier code and see what gets executed.

    >> isn't printf used in C++?
    It can be, but cout is generally used instead.

  14. #14
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    I made some changes:

    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];
    }
    Also, how do I make a +operator() that acts as a unary operator for the string class.

  15. #15
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    You mean like
    Code:
    String String::operator+ (const String & other);
    *edit* Often, such operators are better declared not as members, so that
    you could group those functions with others of similar type -- namely:
    Code:
     String operator+ (const String & left, const char* right);
    Last edited by CodeMonkey; 08-06-2007 at 11:28 PM.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

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, 05: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