Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7844039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:45:19+00:00 2026-06-02T16:45:19+00:00

I was wondering if it was a good practice to use ‘this’ in C++.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T16:45:21+00:00Added an answer on June 2, 2026 at 4:45 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am wondering is it good practice to try to make a view that
Wondering what the best / good way of doing this would be in jQuery.
Wondering if there is a good way to generate temporary URLs that expire in
Is it good practice to create functions which use other functions that you built?
I was just wondering if this approach looks like a good practice for apps
I'm wondering if this is a good way of doing data access, in terms
I was wondering if it is good practice to use the member operator .
I am wondering if it is a good practice to use the same name
I'm wondering about best practice here. Is it good practice for a factory method
Just wondering if the following is considered to be good programming practice or not?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.