Schrustra
Schrustra

Reputation: 19

How do you know what is the current object?

this might be a noobie question but this confuses me, especially when it comes to this pointer. How do I know which object "this" pointer is pointing to? I can provide an example where that confuses me... Say you had this code

#include<iostream>
using namespace std;

class someClass{
int var;
//2 constructors here, one with no arguments, one with giving a value to var(not important)
someClass operator+(someClass object){
someClass rObject;
rObject.var = this->var + object.var //and here is my problem
}
};

int main() {
someClass a(20);
someClass b(30);
someClass c;
c=a+b; //????????
//rest of the code not important

}

In this case the "current object" would be the object a, but how does that make sense if we just switch sides so it's c=b+a the current object would become b... I am confused... What determines the current object?

Upvotes: 0

Views: 988

Answers (3)

MSalters
MSalters

Reputation: 179937

It helps to first consider an easier operator, namely operator +=. It may appear to be a more advanced operator, but that's because we learned arithmetic using +. If you look at your code, you see that you typically need three objects for + c=a+b whereas += just needs two: b += a.

Now in b += a the this pointer should point to the object you're changing, i.e. b. That's the first object. To keep things simple, for all binary operators this points to the first object.

Upvotes: 0

You have chosen the more involved example: operators. In general it is easier to reason about other member functions. Consider:

struct T {
    int value;
    void foo() { std::cout << this->value << '\n'; }
};

int main() {
   T t;
   t.foo();
}

How do you know which is the current object inside foo()? It is the object on the left of the dot operator in the expression, in this case t. (Well, in this case there is no other object at all in the program!)

Now going back to operators, for binary operators in particular, many of them can be implemented in two forms, as a free function taking two arguments or as a member function taking a single argument. There is a direct transformation that the compiler will do for you in both cases:

struct T {
    int value;
    T operator+(T const & rhs) { T tmp; tmp.value = this->value + rhs.value; return tmp; }
};
T operator-(T const & lhs, T const & rhs) {
   T tmp; tmp.value = lhs.value - rhs.value; return tmp;
}

int main() {
   T a, b; a.value = 1; b.value = 2;
   a - b;
   a + b;
}

Then the compiler encounters the expression a - b it searches for the two possible forms of the operator, it finds that it is a free function and it binds lhs to a and rhs to b, transforming the expression into operator-(a, b). In the case of a + b, the compiler again searches for the operator and finds it as a member function, at this point the transformation becomes a.operator+(b) and the this pointer inside the member function refers once again to the object to the left of the dot operator.

Upvotes: 2

John Zwinck
John Zwinck

Reputation: 249303

Non-static class methods have an implicit first argument this of the class type. So when you write:

operator+(someClass object)

You are actually getting this:

operator+(someClass* this, someClass object)

And now you can see: the left-hand side of a binary operator is the first argument, and the right-hand side is the second argument (by convention, and because it makes more sense than the other way around).

Upvotes: 1

Related Questions