views:

293

answers:

10

What is the correct / appropriate English terminology for referring to the "current" object in C++. For example, say you are writing a comment in the body of the implementation of this:

Thing Thing::operator+(const Thing& other)

You have the variable name "other" to use for the other object, but what word / expression do you use to refer to the object that is doing the operation?

+2  A: 

First of all, there's this pointer specifically for that purpose. Therefore it's only natural to refer to the current object as "this object".

sharptooth
+11  A: 

In C++ it's called this object and that's what I'm using.

Edit: I second Neil's comment about operator+() better being a non-member. For me, the canonical form is to implement += as a member (it changes the left-hand side object and should thus have access to it) and implement operator+ on top of it:

inline Thing operator+(Thing lhs, const Thing& rhs)
{
  lhs += rhs;
  return lhs;
}

(Note: I used to pass lhs by const reference, too, and perform the copying within the function, but got convinced a while ago that it's better to pass it as a copy instead.)

sbi
And people discovered the Boost.operators library :D Not only do you write less, but it even enforces this particular behavior.
Matthieu M.
+2  A: 

Could also use "left operand" and "right operand", since your operator is binary.

Blindy
A: 

There is no current object in C++ , but only pointer to object avalilable in the member function is this pointer .

Ashish
While that is technically correct; it's not a particularly helpful way at looking at an object-oriented language.
Williham Totland
I dont understand wht you are saying about...
Ashish
Uh, yes, there **is** a current object. What do you think `this` is pointing at?
Rob Kennedy
+6  A: 

The problem with "this" is that it is a common word in English. As someone that has lectured extensively on C++, I always found it difficult when speaking to get across when I was using "this" to mean the current object. I never came up with a perfect solution, but often wished that BS had used "self" instead of "this".

anon
"the this pointer" is a bit more explicit, but yeah, hardly ideal.
jalf
And it's even worse: the questioner isn't talking about "the this pointer", he's talking about "the referand of the this pointer".
Steve Jessop
true. Would've been nice if it'd been called "self" rather than "this", and been an actual reference instead of a pointer.
jalf
@jalf: It would have been nice, but he added `this` before he added references, and by then it was well established and would have been hard to change.
Jerry Coffin
At least in comments, I find `*this` fairly explicit -- the asterisk makes it fairly clear that I'm not just using "this" in its normal English language sense, and it explicitly refers to the object pointed to by``this`, rather than to the `this` pointer itself.
Jerry Coffin
@Jerry: Actually, I have used `*this`, too, and I did it for the same reason.
sbi
+3  A: 

Either "this", or maybe "this instance". As in:

/** \brief Adds another Thing to this instance.
 * A reference to this instance is returned.
*/
Thing& Thing::operator+(const Thing& other);

The markup is Doxygen, for those not familiar with that.

unwind
+3  A: 

I tend to use the same convention in documentation as I use in code: I leave it implicit. E.g. "Add another thing and return the sum".

When I need to be explicit, it's for comments read by other programmers. They know C++, *this is easily understood. E.g. "Add another Thing to *this by adding all members except Thing::foo which will become 0 (See bug #42)"

MSalters
A: 

In comments, I usually say either "this" or "ourself" (because I generally write comments in first-person plural, but "ourselves" is too weird when there's only one object).

In documentation, I say "the <whatever>". If that's ambiguous (because the function takes another one as a parameter, for instance), I say "this object", or "this <whatever>". <whatever> is either the class name, or some other term used in the documentation to explain what the class represents.

/**
 * An utterly useless write-only counter, with arbitrary initial value.
 */
class Counter {
    unsigned int count;
public:
    /** 
     * Increments the counter
     */
    Counter &operator++() {
        // We need to add 1 to ourself. I found a snippet online that does it:
        ++count;
        // return ourself
        return *this;
    }
    /**
     * Adds together this counter and "other".
     */
    Counter operator+(const Counter &other) const {
        Counter result;
        // new count is our count plus other count
        result.count = count + other.count;
        return result;
    }
};
Steve Jessop
A: 

If you implement an operator outside of the class scope, then the arguments are often named lhs and rhs (left-hand side and right-hand side).

You don't really need a name for the current object, because you already have the thispointer. Inside static methods a pointer an object of the class is sometimes needed (for example to call an instance method). In those situations I use the variable name pThis.

If you do need a name when speaking about it, then "self" makes sense. I sometimes refer to it as "the this object".

StackedCrooked
A: 

Seems to me that if you want to refer to the current object, you can't really go wrong calling it the current object.

Rob Kennedy
And then maybe you can. "The current object" to me (a non-native, so I might be wrong) implies a timespan while it is the current. However, during the same timespan, `other` is just as current.
sbi