i have been given class with int variables x and y in private, and an operator overload function,
class Bag{
private:开发者_如何学Go
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?.
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;
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;
}
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).
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.
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.
精彩评论