plz help me to know..whether this pointer in c++ is public access or private access

Edited 5 Years Ago by cvanithakpm: n/a

plz help me to know..whether this pointer in c++ is public access or private access

Um...what?

It's a pointer to the object, so while the this pointer itself can be described as private, you can still access the address of the object anywhere the object is in scope.

Maybe if you look at this code you'll see what the this pointer is...

#include <iostream>

class myint
{
  
public:
  
  myint(int val):itsvalue(val) {}
  
  int getitsvalue() const { return itsvalue; }
  void* get_this() const { return (void*)this; }
  
private:
  
  int itsvalue;
  
};

int main(int argc, char**argv)
{
  myint me(1234);
  
  std::cout << me.get_this() << std::endl;//find this pointer value
  std::cout << &me << std::endl;//find the address of me
  
  std::cout << me.getitsvalue() << std::endl;
  
  *(int*)&me = 9999;
  
  std::cout << me.getitsvalue() << std::endl;
  
  return 0;
}

Edited 5 Years Ago by gerard4143: n/a

This question has already been answered. Start a new discussion instead.