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

  • Home
  • SEARCH
  • 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 6806721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:44:43+00:00 2026-05-26T19:44:43+00:00

Possible Duplicate: When should I make explicit use of the this pointer? I’m wondering

  • 0

Possible Duplicate:
When should I make explicit use of the this pointer?

I’m wondering about the proper usage of the “this” pointer.

I’ve seen someone create a class constructor with the argument passed variable passed in named ‘data’.
However he had a private member variable named ‘data’ already thus he simply used:

this->data = data;

would have worked to simply use

data = data_in

(if the parameter was named data_in), and no need to invoke the “this” pointer and reference the member type.

Now I’m wondering, is this proper usage? Using this->member to reduce on naming complexity? I mean it works and I see that it accomplishes what was intended but I’m wondering if some of you more experienced C++ guys and girls can say a word or two if this is common practice?

Also, out of curiosity I’ve instrumented the code just to see what happens under the hood, and it seems the “this” pointer is invoked anyhow. I guess that’s the way references to the class object are done anyways.

  • 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-05-26T19:44:43+00:00Added an answer on May 26, 2026 at 7:44 pm

    In most cases, specifically dereferencing the this pointer to access a non-static data-member of the class instance is unnecessary, but it can help with naming confusion, especially when the data-members of the class are defined in a separate header file from the code-module. However, it is necessary to use the this pointer if you are accessing a non-static data-member that is a member of a base-class that is a templated class. In other words, in a situation like:

    template<typename T>
    class base_class
    {
        protected:
            int a;
    };
    
    template<typename T>
    class derived_class : public base_class<T>
    {
        void function()
        {
            a = 5; //this won't work
            this->a = 5; //this will work
        }
    };
    

    you’ll note that you must use the this pointer in order to properly resolve the inherited non-static data-member from the template base-class. This is due to the fact that base_class<T>::a is a dependent name, in this case dependent on the template parameter T, but when used without the this pointer, it is treated as a non-dependent name, and is therefore not looked up in the dependent base-class namespace. Thus without the specific dereference of the this pointer, you’ll end up with a compiler error like “a was not declared in this scope”, or something similar.

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

Sidebar

Related Questions

Possible Duplicate: When should you use 'friend' in C++? I was brushing up on
Possible Duplicate: Why should I use templating system in PHP? I was just curious
Possible Duplicate: Difference between pointer variable and reference variable in C++ When should I
Possible Duplicate: When should you use 'friend' in C++? I have come to a
Possible Duplicate: Allen Holub wrote “You should never use get/set functions”, is he correct?
Possible Duplicate: When do you use the this keyword? In OOP sometimes we can
Possible Duplicate: Java - when to use 'this' keyword Is it generally good convention
Possible Duplicate: When should I use Inline vs. External Javascript? Often Javascript needs to
Possible Duplicate: Should Usings be inside or outside the namespace sa1200 All using directives
Possible Duplicate: Should C++ eliminate header files? In languages like C# and Java there

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.