Thread: Friend operator saving problem

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    45

    Question Friend operator saving problem

    For some reason my operands are not storing the values in the private class, but they are all friends of the class... Where did I go wrong on this code?



    Code:
    class Diamond{
    friend bool operator+(Diamond);
    friend int size(Diamond);
    
    public:
    Diamond();
    Diamond(int);
    
    private:
    int size_of;
    
    };
    
    
    
    
    bool operator+(Diamond d) //expand_size
    {
    int tmp;
    tmp = d.size_of + 2;
    if (tmp < 40 && tmp > 1)
    	{
    	d.size_of = tmp;
    	return true;
    	}
    else
    return false;
    }

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    That is a very uncommon usage of the operator+. Operator+ is a binary operator. That means that it should be a member function that takes a single argument, or even better, a non-member friend that takes two arguments to be added together.

    Assuming for a moment that you know what you are doing, or for some reason you have to do it that way, and you only made typos above, the reason nothing is updated is that you pass the Diamond object to the operator+ function by value. That means a copy is made, so when you set d.size_of equal to tmp, you are changing the value inside the temporary copy.

    By the way, a common way of implementing operator++ is:
    Code:
    class Diamond{
    Diamond& operator++();
    const Diamond operator++(int);
    
    public:
    Diamond();
    Diamond(int);
    
    private:
    int size_of;
    };
    
    Diamond& Diamond::operator++() //expand_size
    {
        int tmp = size_of + 2;
        if (tmp  < 40 && tmp  > 1)
        {
            size_of = tmp;
        }
        return *this;
    }
    const Diamond Diamond::operator++(int) //expand_size
    {
        Diamond oldValue(*this);
        ++(*this);
        return oldValue;
    }

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Or, the way I like to write it
    Code:
    const Diamond Diamond::operator++(int)
    {
        return ++Diamond(*this);
    }

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Except yours won't work properly xErath.

    When you do
    Code:
    obj++;
    it should return a copy of the value before it is incremented. Yours makes a copy and then increments that without ever incrementing the actual object.

    If you don't believe lets test:
    Code:
    #include <iostream>
    
    class Foo {
      int x_;
      public:
        Foo() : x_(0) {}
        Foo(const Foo & f) : x_(f.x_) {}
        const int & x() const { return x_; }
        Foo operator++(int) // post increment
        {
          return ++Foo(*this);
        }
        Foo& operator++() // pre increment
        {
          x_ += 1;
          return *this;
        }
        friend std::ostream& operator<<(std::ostream &o, const Foo &f)
        {
          return o<<f.x_;
        }
    };
    
    int main()
    {
      Foo f;
      std::cout<<f<<std::endl;
      f++;
      std::cout<<f<<std::endl;
      ++f;
      std::cout<<f<<std::endl;
    }
    output:
    Code:
    0
    0
    1
    Now if we change the function to:
    Code:
        Foo operator++(int) // post increment
        {
          Foo temp(*this);
          ++*this;
          return temp;
        }
    we get the proper output of:
    Code:
    0
    1
    2
    Operator+ is a binary operator.
    Just like operator- there is a binary and an unary version. Normally the unary just returns a copy a copy of the object:
    Code:
    Foo Foo::operator+() const
    {
      return *this;
    }
    Of course it could easily be made a non friend, non member function.
    Last edited by Thantos; 11-10-2004 at 12:39 AM.

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Quote Originally Posted by Thantos
    Just like operator- there is a binary and an unary version. Normally the unary just returns a copy a copy of the object:
    Code:
    Foo Foo::operator+() const
    {
      return *this;
    }
    Of course it could easily be made a non friend, non member function.
    Ahh yes, thank you.

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Well.. maybe... But I always write my +, -, *, / operator like that, using +=, -=, *=, /=. The ++ and -- operator simply change the class where there called and not some returned object, so I don't get any problems with those.

    eg:
    Code:
    Class& Class::operator+(Class& cc){
        return Class(*this) +=cc;
    }
    Thanks by the info anyway

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    Class& Class::operator+(Class& cc){
        return Class(*this) +=cc;
    }
    1) The parameter to that function should be const.
    2) You are returning a reference to a local variable
    3) The function itself should be const
    4) It could be a non member non friend function as such:
    Code:
    Class operator+(const Class &c1, const Class &c2)
    {
      return Class(c1) += c2;
    }
    Remind me not to use your code

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    1) I know all that
    2) I wrote that code in the post editor
    3) No special need to be outside the class.
    4) It was only an example...
    Code:
    Class Class::operator+(const Class& cc) const{
        return Class(*this)+=cc;
    }
    I'll just have to remind not to be that much distracted when posting

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    1) Then why didn't you do it correctly?
    2) So did I
    3) No special need to be inside the class

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM