views:

264

answers:

5

hello there, i have been given class with int variables x and y in private, and an operator overload function,

class Bag{
private:
    int x;
    int y;
public:
    Bag();
    ~Bag();
    //.......
    //.....etc
};


Bag operator+ (Bag new) const{
    Bag result(*this);   //what does this mean?
    result.x += new.x;         
    result.y += new.y;
}

What is the effect of having "Bag result(*this);" there?.

+10  A: 

Bag result(*this) creates a copy of the object on which the operator function was called.

Example if there was:

sum = op1 + op2; 

then result will be a copy of op1.

Since the operator+ function is doing a sum of its operands and returning the sum, we need a way to access the operand op1 which is done through the this pointer.

Alternatively we could have done:

Bag result;
result.x = (*this).x + newobj.x; // note you are using new which is a keyword.
result.y = (*this).y + newobj.y; // can also do this->y instead
return result;
codaddict
+1  A: 

The operator+ function returns a copy. The statement:

Bag result(*this);

Is making a copy of this object to return to the caller. According to the signature, it must return a value, so it is making a copy and then adding the new object.

Thomas Matthews
+3  A: 

Firstly, tell the code writer not to use new as the variable name — it's a keyword. Also, remeber to return result;. And either pass by const-reference or directly modify the new bag.


Inside a struct/class, this is a pointer to itself. Therefore, *this is a reference to the whole Bag instance itself.

The statement Bag result(a_bag_reference) will call the copy constructor of Bag, which makes a copy of a_bag_reference into result.

Therefore,

Bag result(*this);

makes a copy of itself, then store into result. This makes the next 2 statements

result.x += new.x;
result.y += new.y;

do not affect the instance itself (i.e. this->x and this->y are kept constant).

KennyTM
+4  A: 

Your code would look like:

class Bag {
public:
  Bag();
  Bag(Bag const& other); // copy ctor, declared implicitly if you don't declare it
  ~Bag();

  Bag operator+(Bag const& other) const;

private:
  int x;
  int y;
};

Bag Bag::operator+(Bag const& other) const {
  Bag result (*this);
  result.x += other.x;         
  result.y += other.y;
  return result;
}

The implicit "current object" for member functions is pointed to by a special value named this. Then *this gets that object (by dereferencing this), and it is used to construct (via the copy constructor) another Bag named result.

I suspect this code is taken from a homework assignment, so you might not be able to use the one true addition operator pattern, but it is common and you should be aware of it:

struct Bag {
  //...
  Bag& operator+=(Bag const& other) {
    x += other.x;
    y += other.y;
    return *this; // return a reference to the "current object"
    // as almost all operator=, operator+=, etc. should do
  }
};

Bag operator+(Bag a, Bag const& b) {
  // notice a is passed by value, so it's a copy
  a += b;
  return a;
}
Roger Pate
That is a dangerous way of writing op+() - the first parameter will be sliced, and if there was intended to be any polymorphic behaviour in the function based on it, there won't be. It's much better to make both parameters const references, and create the copy within the function.
anon
@Neil: Copying within the function also slices; it's much better to use this pattern until you need to change it. Also see http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/ which promotes the same idiom for operator=.
Roger Pate
+1  A: 
David Leonard