我在想在C++中使用“this”是否是一种好的实践。起初我认为应该这样做,因为这样可以明显地表示您引用的东西是当前类的成员,但有时您会得到像下面这样的代码:
我认为如果没有所有“this”,尤其是当它像
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();
}
我认为如果没有所有“this”,尤其是当它像
this->layout->addWidget(this.textArea);
这样使用时,它会看起来更好。我认为代码应该在大多数情况下使用相同的风格,以使其更易于阅读,所以,我应该仅在必要时使用“this”,或者使用“this”以清楚地表示您正在引用同一类的成员,这是一个好习惯吗?
this->myVal = myVal
,它会做正确的事情。否则,我通常不喜欢这样做。 - StilesCrisisthis
,而是内存泄漏。代码有太多实际问题(包括在这种情况下),我们无需担心这些微不足道的小点问题。 - Mankarsethis->
的内容。这就是我所做的。 - Mr Lister