Use of "this". Advice?

I'm better at Java and Objective-C than CPP, so I don't feel completely sure about whether this usage is good or bad.

I ran into a CPP project in which the authors profusely use "this" in methods. I'm concerned because I suspect all of these "this->" usages may make the program run more slowly. Here's an example of a constructor

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Model::Model()
{
        this->lconditional = false;
        this->lneedChain = false;
        this->lneedScores = false;
        this->lneedDerivatives = false;
        this->lparallelRun = false;
        this->linsertDiagonalProbability = 0;
        this->lcancelDiagonalProbability = 0;
        this->lpermuteProbability = 0;
        this->linsertPermuteProbability = 0;
        this->ldeletePermuteProbability = 0;
        this->linsertRandomMissingProbability = 0;
        this->ldeleteRandomMissingProbability = 0;
        this->lsimpleRates = 0;
        this->lmodelType = NORMAL;
}

All of these variables are listed in the private section of the Model class's definition.

I think that all of these "this->" are not needed. All of those variables are instance variables, and inside the class's constructors and member functions, all of those variables are directly available.

Am I understanding "this" correctly?



pj
It's exactly the same. If you don't put this-> the compiler will assume it if needed.
this is a parameter that is invisibly passed to each memberfunction. It is always used to access member variables whether written or not
You don't need to do that, and shouldn't unless there's a compelling reason to.

Perhaps you just need some naming convention to identify member data.
Somewhat related point: you should use initializer lists where possible
Topic archived. No new replies allowed.