5

I made a simple class to represent a door. To return the variables, I'm accessing them with the this pointer. With respect to just accessing variables, what's the difference between accessing them with the this pointer and without?

class Door
{
protected:
    bool shut; // true if shut, false if not shut
public:
    Door(); // Constructs a shut door.
    bool isOpen(); // Is the door open?
    void Open(); // Opens the door, if possible. By default it
    // is always possible to open a generic door.
    void Close(); // Shuts the door.
};
Door::Door()
{}
bool Door::isOpen()
{
    return this->shut;
}
void Door::Open()
{
    this->shut = false;
}
void Door::Close()
{
    if(this->isOpen()) this->shut = true;
}

There may or may not be a difference here, but what about for more complex classes?

dukevin
  • 22,384
  • 36
  • 82
  • 111

5 Answers5

10

Nothing. The this pointer is automatically added if you exclude it.

You only have to use it if you're doing something like this:

void Door::foo(bool shut)
{
    this->shut = shut; // this is used to avoid ambiguity
}

More usages


A brief overview:

Think of methods as functions that pass a pointer as their first argument.

void Door::foo(int x) { this->y = x; } // this keyword not needed

is roughly equivilent to

void foo(Door* this_ptr, int x) { this_ptr->y = x; }

Methods just automate this.

Community
  • 1
  • 1
Pubby
  • 51,882
  • 13
  • 139
  • 180
  • So it's only used to disambiguate between 2 variables of the same name? – dukevin Nov 03 '11 at 21:54
  • 2
    There are other uses, e.g. when you have a function taking a `Door*` you can pass it `this`. But +1 for showing when to use `this->`. – Fred Foo Nov 03 '11 at 21:56
  • 1
    That's not the only case. It also effects the name lookup in template code. – Gene Bushuyev Nov 03 '11 at 21:57
  • @dukevin Or you can have a function which returns this and use it for function chaining. – FailedDev Nov 03 '11 at 21:58
  • 4
    Extreme pedantry alert: it can become an issue when using templates, because it can be the case this->member is a dependent name, and member is not a dependent name. – jwismar Nov 03 '11 at 21:59
3

There's no difference.

When you write sane C++, you shouldn't have to say this at all except in very specific situations. (The only ones I can think of are binding pointers-to-member-function, passing the instance pointer to some other object, and some situations involving templates and inheritance (thanks to Mooing Duck for that last example).)

Just give your function arguments and local variables and member variables sensible names so that you don't get any ambiguities.

There's a slew of more recent quasi-object-oriented languages which have made the words "this" and "new" all but synonymous with "I'm using objects" for younger generations, but that is not the C++ idiom.

Community
  • 1
  • 1
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • 1
    You need it when you inherit from a class that depends on a template parameter/class – Mooing Duck Nov 03 '11 at 22:01
  • @MooingDuck: that's a good point. You should post that as an answer, as that's a very interesting subtlety. – Kerrek SB Nov 03 '11 at 22:02
  • ^^ That. Plus sometimes it can make it more clear to use `this` when using member variables in certain functions, to explicitly distinguish them from variables local to that function. I usually prefix my local variables (at least private ones) with `_` anyway so I don't have that problem. – AusCBloke Nov 03 '11 at 22:03
  • @AusCBloke: Well, that's strictly a matter of style, and you're entirely welcome to adopt whichever conventions you find make your code most readable and maintainable. I'd say that sticking to a consistent naming scheme that distinguishes local, member and argument variables is preferable because it makes the code less noisy and less distracting, but ultimately it's your choice. – Kerrek SB Nov 03 '11 at 22:06
  • 2
    I know why some people use superfluous `this`, because as soon as they type `this->` IDE brings up a list of suggested names. Probably doesn't work with templates anyway, because IDE cannot do anything about two phase lookup. – Gene Bushuyev Nov 03 '11 at 22:08
  • The other obvious place you use `this` explicitly `return *this` in an `operator=`. – Jerry Coffin Nov 03 '11 at 22:22
1

In your case there is no difference. Only more typing work.

Sergei Nikulov
  • 5,029
  • 23
  • 36
1

This whole thing seems to be an exercise in superfluous typing. As far as I can see, Close can be condensed down to:

void Door::Close() {
    shut = true; 
}

Doing the assignment even when it's unnecessary is much simpler than testing and setting only if it's currently false.

It's also worth mentioning (IMO) that this comment:

Door(); // Constructs a shut door.

Does not seem to fit with the implementation:

Door::Door()
{}

If you want the default ctor to initialize shut to true, you need/want to add some code to do that.

Worse, your IsOpen seems to get things exactly backward:

bool Door::isOpen()
{
    return this->shut;
}

If it's shut it's not open, and vice versa.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
0

There are no difference, except more type and the noise introduced by this->.

void Door::Close()
{
    if(isOpen()) shut = true;
}

is more readable them this :

void Door::Close()
{
    if(this->isOpen()) this->shut = true;
}

but that's only personal preference, and a matter of a style.

BЈовић
  • 62,405
  • 41
  • 173
  • 273