views:

325

answers:

7
+3  A: 

When there is an ambiguity between, say, a function parameter and an instance variable.

Of course such ambiguity should be avoided! It might be preferable to change the function parameter name instead of incurring overhead (i.e. prefixes) for all access to instance parameters though...

jldupont
Wouldn't creating such an ambiguity be bad style?
Heath Hunnicutt
Use a naming convention that prevents such ambiguity, e.g. precede all data members with m_.
Patrick
m_ is horrible, as is any other prefix.
Finglas
Personally I think things like "precede all data members with m_" just causes pointless noise, just like hungarian notation. It fixes this "problem", but seems silly to me.
Herms
Why prefer a naming convention to a language feature?
Jeff Sternal
@Patrick: Why? Do you also avoid namespaces and instead prefix all your names with some kind of pseudo-namespace, as in `boost_shared_ptr` instead of `boost::shared_ptr`? The nice thing about `this->` is that it can be omitted when it's unnecessary, yielding cleaner code. You can't omit a hardcoded `m_` prefix.
jalf
No I don't avoid namespaces, I don't prefix all names to prevent namespaces, I only use m_ for data members. The point is that you want clear, readable code. If you omit prefixes like m_ and can still keep clear code, fine, then don't use it. Personally, I find my code clearer if I prefix data members with m_.
Patrick
+3  A: 

Personally I never use this, except:

  • when I need to pass 'this' as an argument to a method of another class
  • in the implementation of the assignment operator
Patrick
+3  A: 

While this is a totally subjective question, I think the general C++ community prefers not to have this->. Its cluttering, and entirely not needed.

Some people use it to differentiate between member variables and parameters. A much more common practice is to just prefix your member variables with something, like a single underscore or an m, or m_, etc.

That is much easier to read, in my opinion. If you need this-> to differentiate between variables, you're doing it wrong. Either change the parameter name (from x to newX) or have a member variable naming convention.

Consistency is preferred, so instead of forcing this-> on yourself for the few cases you need to differentiate (note in initializer lists this is completely well-defined: x(x), where the member x is initialized by the parameter x), just get better variable names.

This leaves the only time I use this: when I actually need the address of the instance, for whatever reason.

GMan
Clearly "m_" is sort of a love-it-or-hate-it thing. I love it myself. Having p_Thing, l_Thing and m_Thing referenced in the same method is not unusual where Thing is simply the logical name for all three. It's common enough to have at least two clash (getters and setters, for instance) that it makes sense to me to just apply the convention all the time.
Steve314
A: 

It's usable when you have variables in a scope "above" the one you are working with.

int i;
public void foo() {
    int i;
    i = 3; // assign local variable
    this->i = 4; // assign global variable
}

Other than accessing variables in another scope, I myself agree with your "minimalistic choice". Less is more. :-)

Patrick
This situation should be avoided.
John Dibling
Why declare a local variable that (by common convention) is falsely claiming to be a member variable?
Steve314
Of course you shouldn't name a local variable like that, it was just an example.
Patrick
A: 

I like to use it for clarification, as when accessing members that were inherited. It reminds the reader where the variable came from if you don't have a naming convention that conveys that information.

You must use the this pointer when:

  • Returning the current object.
  • Setting up relations between objects (passing this into a constructor or setter)
  • Checking for self reference: this != argPtr
thebretness
+1  A: 

I can only recall doing it with

delete this;
Tim
That line is kind of terrifying. Then again, I remember having to do `if(this == null)` before to help track down some weird bug.
Herms
I used that one or two times in my code too. Ph34r m3! :)
Eugene
This is standard practice in reference-counted enviroments like COM.
Georg Fritzsche
@Herms. Actually it is pretty common and not at all problematic if used correctly.
Tim
oh I know it's actually useful at times. It's just one of those things that makes my brain go "gwah?!" when I first see it.
Herms
A: 

For me, it depends. If it is a short function or variable, I just type it in (e.g. mCount). Most of the time, however, I use very descriptive member variable and function names (e.g. mExclusivelyLockedDigitalIOList ). In those instances, I tend to use the this pointer to have Visual Studio's IntelliSense finish my typing for me. Saves on keystrokes and spelling mistakes.

bsruth