0

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

Edited by cvanithakpm: n/a

3
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by gerard4143
0

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

Um...what?

0

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.

0

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 by gerard4143: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.