tags:

views:

618

answers:

15

I programmed in C++ for many years and I still have doubt about one thing. In many places in other people code I see something like:

void Classx::memberfunction()
{
    this->doSomething();
}

If I need to import/use that code, I simply remove the this-> part, and I have never seen anything broken or having some side-effects.

void Classx::memberfunction()
{
    doSomething();
}

So, do you know of any reason to use such construct?

EDIT: Please note that I'm talking about member functions here, not variables. I understand it can be used when you want to make a distinction between a member variable and function parameter.

EDIT: apparent duplicate: http://stackoverflow.com/questions/333291/are-there-any-reasons-not-to-use-this-self-me

+2  A: 

I can think of readability like when you use additional parenthesis to make things clear.

Edouard A.
+1  A: 

I don't think it makes a difference to the compiler, but I always write this-> because I believe it makes the code self-documenting.

Ray Hidayat
+2  A: 

I think it is mainly as an aid to the reader. It makes it explicit that what is being called is a method on the object, and not an ordinary function. When reading code, it can be helpful to know that the called function can change fields in the current object, for instance.

unwind
+17  A: 

If there is another variable in the same scope with the same name, the this-> will remove the ambiguity.

void Bar::setFoo(int foo)
{
    this->foo = foo;
}

Also it makes it clear that you're refering to a member variable / function.

drby
Using it like this makes it a band-aid for a bad naming convention, in my opinion. It clutters up the code needlessly.
MadKeithV
Just answering the question.
drby
I did not ask a question about variables, that is a known issue. I asked about member functions.
Milan Babuškov
@MadKeithV: this-> is a language-and-tool-supported naming convention, unlike for instance m_ prefix.
MSalters
And it still does not make it clear what's the scope of foo on RHS.
Milan Babuškov
+1  A: 

Disambiguation: in case you have another similar naming function/variable in the same namespace? I've never seen usage for any other reason.

dirkgently
+1  A: 

It's your own choice. I find it more clear when you use this. But if you don't like it, you can ommit it.

Ikke
+2  A: 

As "code reason", to distinguish a local parameter or value (that takes precedence) from a member:

class Foo
{
    int member;
    void SetMember(int member)
    {
       this->member = member;
    }
}

However, that's bad practive to begin with, and usually can be solved locally.

The second reason is more "environment": it sometimes helps Intellisense to filter what I am really looking for. However, I also thing when I use this to find the member I am looking for I should also remove this.

So yes, there are good reasons, but they are all temporary (and bad on the long run).

peterchen
I disagree that this is necessarily a bad practice. If you have a good, clear, and umbiguous name for an object or variable... why change it?
TK
IMO coding standard should make public properties vs. internal members vs. local variables/parameters unambigous. As much as i hate typing `m_`, there is a good reason for it.
peterchen
To clarify: I think it's ok when it happens, but when it happens often, you likely have a readability problem and an invitation to attention slip bugs. Now, your standard might declare 'this->' to be used always as 'the distinguisher', but I don't think that's a good choice.
peterchen
+1  A: 

This is really a matter of style and applies to many other languages such as Java and C#. Some people prefer to see the explicit this (or self, or Me, or whatever) and others do not. Just go with whatever is in your style guidelines, and if it's your project, you get to decide the guidelines.

HTH, Kent

Kent Boogaart
And some languages (PHP, Python) even enforce it. One of the reasons I hate doing OO in them.
Milan Babuškov
+1  A: 

I prefer it without the explicit this pointer as well. For method calls it doesn't add a lot of value, but it helps distinguish local variables from member variables.

Brian Rasmussen
+1  A: 

This has been asked a couple of times, but it's hard to search for it because "this" seems to be stop word for the search.

Here's my own question: http://stackoverflow.com/questions/333291/are-there-any-reasons-not-to-use-this-self-me

DR
+2  A: 

This is done to be explicit about the fact that the variable being used is a member variable as opposed to a local or global variable. It's not necessary in most cases, but being explicit about the scope could be helpful if you've trumped the variable with a declaration of the same name in a tighter scope.

At companies I've worked at, we just prepended "m_" to member variables. It can be helpful sometimes, and I much prefer it to using "this->".

Edit: Adding a link to the GCC docs, which explain a case where using this-> is necessary to get a non-dependent lookup to work correctly.

Dan Olson
+16  A: 

The only place where it really makes a difference is in templates in derived classes:

template<typename T>
class A {
protected:
  T x;
};

template<typename T>
class B : A<T> {
public:
  T get() {
    return this->x;
  }
};

Due to details in the name lookup in C++ compilers, it has to be made explicitly clear that x is a (inherited) member of the class, most easily done with this->x. But this is a rather esoteric case, if you don't have templated class hierarchies you don't really need to explicitly use this to access members of a class.

sth
+1  A: 

I can't quite remember the exact circumstances, but I've seen (very rare) instances where I had to write "this->membername" to successfully compile the code with GCC. All that I remember is that it was not in relation to ambiguity and therefore took me a while to figure out the solution. The same code compiled fine without using this-> in Visual Studio.

Adrian Grigore
It has to do with dependent template parameters, this link gives a good explanation: http://gcc.gnu.org/onlinedocs/gcc/Name-lookup.html
Dan Olson
+3  A: 

To guarantee you trigger compiler errors if there is a macro that might be defined with the same name as your member function and you're not certain if it has been reliably undefined.

No kidding, I'm pretty sure I've had to do exactly this for that reason!

Andy Dent
Nobody seems to be mentioning that another way of resolving member names is by using the http://en.wikipedia.org/wiki/Scope_resolution_operator. E.g. Classx::doSomething();
Rehno Lindeque
If applied to a function it has the effect of guaranteeing which version of a possibly virtual function is invoked, preventing polymorphism and commonly used to call a parent of an overriden function. It is NOT a safe equivalent of this->doSomething().
Andy Dent
That's true, I'd forgotten about this case! Thanks Andy.
Rehno Lindeque
A: 

I will use it to call operators implicitly (the return and parameter types below are just dummies for making up the code).

struct F {
  void operator[](int); 
  void operator()();

  void f() {
    (*this)[n];
    (*this)();
  }

  void g() {
    operator[](n);
    operator()();
  }
};

I do like the *this syntax more. It has a slightly different semantic, in that using *this will not hide non-member operator functions with the same name as a member, though.

Johannes Schaub - litb
What do you mean "`*this` will not hide non-member operator functions with the same name as a member"? Can you provide an example?
rlbond
Example: `struct A { void operator*(); void f(); }; void operator*(A, A); void A::f() { A a; operator*(a, a); }` <- this is ill-formed, as the member operator* hides the global operator*. But if you write `a * a` (the expression syntax), it will work fine, as lookup will be separate for members and nonmembers.
Johannes Schaub - litb
(The Standard shows a better example at 13.3.1.2/10).
Johannes Schaub - litb