Thread: overloading operators

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    137

    overloading operators

    Code:
    class String
    {
      protected:
        std::string string;
      public:
        String operator= (char *str);
    }
    
    String String::operator= (char *str)
    {
        string = str;
    }

    I want this to function like the string class that when you create a string you can assign a char pointer to it.

    For instance, I want to be able to do:
    Code:
    String s;
    s = "Hello, world!";

    However, when I try to run that my program crashes. Is there something I'm doing wrong?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    you dont get any warnings after compiling? do you have to change the parameter to 'const char *str'? if that doesnt help could you post the complete code so i can try it?

  3. #3
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Did you #include <string>?
    I couldn't even get the code to compile.

    Operator= should return a String reference, using the this pointer.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    I included <string>

    Here is everything you should need that I have:
    Code:
    class String
    {
      protected:
    	std::string string;
    
      public:
    	String operator= (char *str);
    };
    
    String String::operator= (char *str)
    {
    	string = str;
    }
    
    int main(int argc, char *argv[])
    {
    	String s;
    	s = "Hello, World!";
    	return 0;
    }

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    your problem is solved by doing what MacNilly suggested stated

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    He said I should include <string> and I did.

    "Operator= should return a String reference, using the this pointer."

    I'm not entirely sure what that means or what I should do with it.

    I just want to be able to set the std::string inside of the String class equal to a value as a regular std::string can. Am I on the right track and if MacNilly told me how to fix it. I don't really understand.

    Sorry, but could someone elaborate a little.

    Thank you.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    he said you should return 'this' from your operator= function, just like how you 'return 0' in main, but 'this' instead of '0'. in doing so, you have to change the return type (from String) so that it returns a pointer to a string, so change it to String*.

    hopefully you can get it!

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    That works, but I don't understand the logic at all. I would really like to overload a lot of operators in the program I'm working on right now. So, if anyone could explain how this works or direct me to a good article about this that would be great.

    I searched google, but I didn't really get anything direct.

  9. #9
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Code:
    your_String = "static string literal";
    Is the same as
    Code:
    your_String.operator=("static string literal");
    Now, if you have
    Code:
    my_String = your_String = "static string literal";
    Then you really have
    Code:
    my_String.copy_constructor(your_String.operator=("static string literal"));
    The only argument the default copy constructor takes is a reference to an object of the class's type (String). So, have your operator=() function return a reference to a String. Which String? Well, which object does it make sense to return? this object. So, we return *this (the actual object calling the operator), for which a reference to is returned by the function.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Why wrap a class around just a std::string object, when you can just use a std::string object? I really don't see the point!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    Because I'm using it to render strings. And if I wouldn't have the class you would have to have a function with 4 arguments instead of string.draw(x, y);

  12. #12
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It still seems a bit dubious that you'd need to reimplement a lot of std::string functionality to render it. Shouldn't you be able to do draw(string, x, y)?

    Other than that, is a function with 4 arguments that bad? You could also reduce the number of arguments by packing x and y into a Point struct. (draw(string, place, colour) for example)
    Last edited by anon; 08-13-2007 at 11:23 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  13. #13
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    Right. But then I would have to have the font rendering strings. Instead of the strings rendering themselves. Which wouldn't be so bad if my engine wasn't set up differently. And if I did that I wouldn't be able to build the texture for the string before hand if it was a constant string.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about overloading operators
    By *DEAD* in forum C++ Programming
    Replies: 9
    Last Post: 05-08-2008, 10:27 AM
  2. Replies: 16
    Last Post: 10-27-2007, 12:42 PM
  3. Overloading fstream's << and >> operators
    By VirtualAce in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2007, 03:17 AM
  4. operators overloading
    By waqasriazpk in forum C++ Programming
    Replies: 1
    Last Post: 07-26-2002, 01:05 AM
  5. Overloading operators...
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2001, 08:24 PM