Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C++ is it a good practice to use 'this'?

I was wondering if it was a good practice to use 'this' in C++. At first I thought I should be because that way you make obvious that the thing you are referring is a member of the current class but sometimes you end with code like:

Document::Document(QWidget *parent) : QWidget(parent)
{
    this->file = 0;
    this->layout = new QGridLayout(this);
    this->layout->setSpacing(2);
    this->layout->setMargin(0);
    this->setLayout(this.layout);
    this->textArea = new QTextEdit(this);
    this->textArea->setLineWrapMode(QTextEdit::NoWrap);
    this->textArea->setAcceptDrops(true);
    this->textArea->setAcceptRichText(true);
    this->textArea->setUndoRedoEnabled(true);
    this->textArea->setFont(QFont("Mono" , 11));
    this->layout->addWidget(this->textArea);
    this->textArea->show();
    this->textArea->setFocus();
}

I thing this would look a lot better without all the 'this' specially when it is used like this->layout->addWidget(this.textArea);. And I think the code should use the same style in most cases to make it easier to be read so, should I use 'this' just when it is necessary or is it a good practice to use it to make clear that you are referencing a member of the same class.

like image 384
Topo Avatar asked Apr 25 '12 06:04

Topo


People also ask

Is it good practice to use this keyword?

Yes, this keyword is very useful when you are working with instance. The this keyword refers to the current instance of the class. It can be used to access members from within constructors, instance methods, and instance accessors.

Should we use this in C#?

The This ReferenceThe this keyword is used to reference the current instance of a class, or an object itself, if you will. It is also used to differentiate between method parameters and class fields if they both have the same name.

Is this good to use C++?

C++ is one of the standard languages within back-end development. It's an extremely fast and efficient language. Many tools and frameworks rely on the speed and efficiency of C++. It's in high demand now, and it will remain in high demand in 2022 because of its reliability, performance, and efficiency.


2 Answers

There's no single answer to this. It depends on the context, and it depends on who you ask.

Some people use this all the time. Some never use it. Some instead rename all member variables with a special prefix (mFile instead of file).

I prefer to only use the this prefix when it's necessary to avoid ambiguity.

In well written code, it is usually obvious whether you're referring to a local or a member variable, and so this doesn't really do much. But sometimes it can be hard for the reader to determine if a variable is a class member or not. Then this is a good way to clarify.

like image 148
jalf Avatar answered Sep 16 '22 11:09

jalf


If using this makes it easier to read (easier to understand, that is) then the extra typing is well worth the effort.

In a complex program every line of code will be written once but read many times (and by different programmers), so it's a good investment.

One of the most readable languages I know, Python, forces you to write self.x (the equivalent of this->x of C++) every single time and that is not an issue at all. The problem with it is that in Python when you write slef.x instead of self.x the error is caught at runtime, but this is a different unrelated issue...

like image 45
6502 Avatar answered Sep 20 '22 11:09

6502