#1
  1. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    16

    Can not quite get overloading operators right.. instrinsic type + udt


    I have this working, type = type + int; type being my class.., but how can I get type = int + type to work?


    Also, does anyone have any overloading operator articles/tutorials that they recommend I read? The ones I've read do not go into detail or anything, they just show to how overload +, ++.


    If I want to overload =.., what is the prototype?

    type operator= (const type& rhs);
    or
    type operator= (type& lhs, const type& rhs);

    in the first prototype, I would edit 'this' to edit the left hand side of lhs = rhs, correct?
    and the return would be *this?
    I want to be able to do type = type = type.., like int = int = int.


    Do destructors get called when you are writing an expression that is using overloaded operators?
    like in c = a + b;
    Last edited by movEAX_444; January 29th, 2004 at 11:37 AM.
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    248
    I second the motion for a good location for doing non-run-of-the-mill operator overloading. I have been doing things by trial and error and even though I have "The C++ Programming Language, 3rd Ed", I am not very much smarter.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    16
    Do I return a reference or a copy of the object?

    Code:
    frac operator++() {
    		++this->_n; 
    		return *this;  
    	}
    This works fine. I read that you should return frac& though.. If I do, then wouldn't the object receiving the reference be the same as "this".. so editing one edits the other?
  6. #4
  7. Left due to despotic ad-min
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Posts
    1,044
    Rep Power
    18

    Here's an example of overloading operator+


    PHP Code:

    #include <iostream>

    class Something
    {
      public:
         
    Something(int x 0);
         ~
    Something();

        
    // compiler will provide valid copy constructor and assignment
        //   operators in this case, as memberwise copy does the
        //   right thing for this class.


        
    friend Something operator+(const Something &, int);
        
    friend Something operator+(int, const Something &);

        
    void Print();

      private:

        
    int data;
    };

    Something::Something(int x)
    {
        
    data x;
    }

    Something::~Something()
    {
    }

    Something operator+(const Something &lhsint rhs)
    {
          
    Something retval(lhs.value rhs);
          return 
    retval;
    }

    Something operator+(int lhs, const Something &rhs)
    {
          
    Something retval(lhs rhs.value);
           
    //  alternative implementation in this example:
           //   Something retval = operator+(rhs, lhs);
           //     (call other operator+()).
          
          
    return retval;
    }

    void Something::Print()
    {
        
    std::cout << data << '\n';
    }

    int main()
    {
         
    Something x(1);

         
    Something y 3;
         
    y.Print();
         
    10 x;
         
    y.Print();
      

    A few notes;

    1) I've done both operator+() functions as friends. That is not technically necessary if the class provides sufficient accessors (eg a means to get/set the value of the "data" member).

    2) The first version of operator+() I've given (the one with a Something as lhs) is an alternative to the common form where operator+() is a member function. If you provide this version of operator+(), for Something on the lhs, you can't provide the member version. And vice versa. If you do, the result is ambiguity (in this example the compiler would be unable to decide which one to call when with the expression y = x + 3;).

    3) There is no way of having the version with an int on the LHS as a member function. The only way of implementing an operator+() in this case is as a non-member function.
    Last edited by grumpy; January 31st, 2004 at 12:27 AM.

IMN logo majestic logo threadwatch logo seochat tools logo