8

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?.

silent
  • 2,836
  • 10
  • 47
  • 73

5 Answers5

10

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
  • 445,704
  • 82
  • 492
  • 529
5

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;
}
Community
  • 1
  • 1
  • 1
    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. –  Mar 28 '10 at 09:12
  • @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=. –  Mar 28 '10 at 14:53
4

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
  • 510,854
  • 105
  • 1,084
  • 1,005
2

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
  • 56,849
  • 17
  • 98
  • 154
2

Bag result(*this); is declaring a variable result and invoking its copy constructor.

You can imagine C++ automatically declares a default copy constructor for all classes. Its job is simply to initialize an object using another object:

Bag::Bag(Bag const& src) {
   x = src.x;
   y = src.y;
}

The expression *this may look a little unsettling, but is just the usual horror of C++ when you deal with & parameters.

David Leonard
  • 1,694
  • 1
  • 17
  • 14