Thread: Operator Overloading - Can This Be Done?

  1. #16
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    I started out using constant chars, but then I kept getting grief from my compiler about converting const char* to char*, so i just made everything non constant. Anyway I think I'm going to scrap this as its not doing anything useful. But I would still like to know how I could add as many things as I wanted to a string like:

    my_string = "jfh" + number + "djfbjsd" + another_string;

    The equals operator comes last right? So where would I store the things as they are getting added together each step? I'd basically have to do that and then just set my_string to the result. Like I said, I got no idea how I could go about this.

    Cheers.

    [edit] Just read your edit daved. thanks, i'll see what I can come up with. [/edit]
    Last edited by mike_g; 09-19-2007 at 05:19 PM.

  2. #17
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The const char* is less important than const String&, but you should put both there.

    You need your operator+ to work if you want to do anything like my_string = "jfh" + number + "djfbjsd" + another_string; and that means fixing your consts.

    The reason is that when you have a bunch of things added together like that, each addition is done one at a time. The first two pieces are added and stored in a temporary string, which is then added to the third piece and that result is stored in another temporary string, and that is then added with the fourth piece, and so on. A temporary object cannot be modified by the function, which is why the reference needs to be const.

    So to solve your problem, get your operator+(const String& left, const String& right) working, then strings like this "jfh" will work automatically. Adding numbers is more difficult like I mentioned in an edit above.

  3. #18
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Any advice on how I can go about this?

    just an example using existing classes:

    Code:
    struct cat
    {
            cat(void)
            {   }
    
            template <typename Type>
            cat(Type const & rhs)
            {
                    std::stringstream ss;
                    ss << rhs;
                    buf = ss.str();
            }
    
            operator
            std::string & (void)
            {
                    return buf;
            }
    
            operator
            std::string const & (void) const
            {
                    return buf;
            }
    
            std::string buf;
    };
    
    cat
    operator + (cat const & lhs, cat const & rhs)
    {
            return lhs.buf + rhs.buf;
    }
    
    int 
    main(void)
    {
    	std::string msg = cat() + 1 + 2 + 3 + "..." + 'a' + 'b' + 'c';
    	std::cout << msg << std::endl;
    	return 0;
    }
    Last edited by Sebastiani; 09-20-2007 at 01:47 PM. Reason: formatting
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #19
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I suggest losing the old C throwback of (void), just use ().

    Afaik, there is no such thing of a templated constructor, so I expect that it would not work.

    Last of all, what's stopping the compiler from interpreting 1 + 2 + 3 as 6? (Note I'm not sure if it will or not, just asking)
    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"

  5. #20
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Last of all, what's stopping the compiler from interpreting 1 + 2 + 3 as 6? (Note I'm not sure if it will or not, just asking)
    The compiler goes from left to right. The return type-value becomes a new left operand. So, if there has been a + overload returning a String before, these 1 + 2 + 3 will be treated as right operands - in turn - where the left one is a String.

    You can't start with 2 ints (then they'll be summed as ints) unless you help a bit with parenthesis. Crude example:

    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    std::string to_string(int n)
    {
        std::stringstream ss;
        ss << n;
        return ss.str();
    }
    
    struct MyString
    {
        std::string s;
        MyString(const std::string& p = std::string()): s(p) {}
    };
    
    MyString operator+ (const MyString& a, const MyString& b)
    {
        return MyString(a.s + b.s);
    }
    
    MyString operator+ (const MyString& a, int i)
    {
        return MyString(a.s + to_string(i));
    }
    
    MyString operator+ (int i, const MyString& a)
    {
        return MyString(to_string(i) + a.s);
    }
    
    int main()
    {
        MyString a("TEXT"), b("END"), c, d;
        c = 1 + 2 + a + 5 + 6 + b;
        d = 1 + (2 + a) + 5 + 6 + b;
        std::cout << c.s << '\n'  //3TEXT56END
            << d.s << '\n'; //12TEXT56END
    }
    P.S Sebastian's code compiles and runs too, so there is a constructor that takes template parameters.
    Last edited by anon; 09-20-2007 at 04:10 PM.
    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).

  6. #21
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by iMalc View Post
    Afaik, there is no such thing of a templated constructor, so I expect that it would not work.
    Sure there is, as well as templated member functions. A given compiler might not support it, though.

  7. #22
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by brewbuck View Post
    Sure there is, as well as templated member functions. A given compiler might not support it, though.
    Aw shoot, you're right. My memory is failing me.
    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"

Popular pages Recent additions subscribe to a feed