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 8572689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:04:49+00:00 2026-06-11T19:04:49+00:00

How does the this pointer behaves when used inside a base class method: class

  • 0

How does the this pointer behaves when used inside a base class method:

class Base{
public:
   int a;
   Base() : a(5) {}
   void func(){
       std::cout << " value is : " << this->a << std::endl;
   }
};

class Derived : public Base{
private:
   int a;
public:
   Derived() : a(1){}
   void func1(){
       std::cout << " value is : " << this->a << std::endl;
   }
};

int main(){
   Derived d;
   d.func();
   d.func1();
}

the output of the code is :

value is : 5

value is : 1

As i am using the same object to call both the functions. So will the value of this pointer differ in methods for base and derived 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-11T19:04:50+00:00Added an answer on June 11, 2026 at 7:04 pm

    this->a is equivalent to a in that context, so it has nothing to do with the base pointer.

    The member a is resolved statically, and the derived class hides the base class member, since they’re both named a.

    To check the this pointer itself, you can print it directly:

    std::cout << this;
    

    It will be the same for both objects.

    The main thing to take from this is that Base::a and Derived::a are different. Try the following in Derived:

    void func1(){
        std::cout << "derived value is : " << a << std::endl;
        std::cout << "   base value is : " << Base::a << std::endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For this code: class B1{ public: virtual void f1() {} }; class D :
I'm trying to understand the events pointer in epoll_wait. When epoll_wait returns, does this
This concept seems to trouble me. Why does an NSError object need its pointer
Does this function has the same behavior that memset ? inline void SetZeroArray( void
Does this code cause a memory leak: int main(){ int * a = new
How does the this pointer points to the object itself? Is it a java
I want to const declare the this pointer received as an argument. static void
Does using this pointer adds another operation to the program at runtime? Just to
I came across this strange code snippet which compiles fine: class Car { public:
I have a confusion related to this program. #include <stdio.h> int main(void) { int

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.