944,043 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2993
  • C++ RSS
Oct 20th, 2005
0

Accessing Private data memebrs outside the class

Expand Post »
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class emp
  6. {
  7. private :
  8.  
  9. int i ;
  10. int j;
  11.  
  12. public :
  13.  
  14. emp( )
  15.  
  16. {
  17.  
  18. i = 10 ;
  19. j = 50 ;
  20.  
  21. }
  22.  
  23. void display()
  24. {
  25. cout<<endl<<i<<" "<<j<<endl;
  26. }
  27. } ;
  28.  
  29. int main( )
  30.  
  31. {
  32. emp *p = new emp ;
  33.  
  34. p->display();
  35.  
  36. int *pi = (int*) p ;
  37.  
  38. cout << *pi ;
  39.  
  40. *pi = 20 ;
  41.  
  42. p->display();
  43.  
  44. delete p;
  45.  
  46. return 0;
  47. }
I have two questions...

First: Isn't the above code shows flaw/hole in the language??I am able to access the private member of the class through typecasting

Second:If you see the output of this code...it comes out to be
10 50
10
20 50
How can i change the private data member j through the typecasted pointer
Similar Threads
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
Oct 20th, 2005
0

Re: Accessing Private data memebrs outside the class

>Isn't the above code shows flaw/hole in the language??
No. If C++ were designed to avoid both stupid breaking of the rules and willful/malicious breaking of the rules, the language would be a beast that nobody wanted to use. C++ protects you from your stupidity, but if you know how to sidestep the protection, that's your business.

>How can i change the private data member j through the typecasted pointer
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class emp {
  6. int i;
  7. int j;
  8. public:
  9. emp(): i ( 10 ), j ( 20 ) {}
  10. void display()
  11. {
  12. cout<< i <<' '<< j <<'\n';
  13. }
  14. };
  15.  
  16. int main()
  17. {
  18. emp e;
  19. unsigned char *p =
  20. reinterpret_cast<unsigned char*> ( &e );
  21. int *pi =
  22. reinterpret_cast<int*> ( p );
  23. int *pj =
  24. reinterpret_cast<int*> ( p + sizeof ( int ) );
  25.  
  26. e.display();
  27. ++*pi;
  28. --*pj;
  29. e.display();
  30.  
  31. return 0;
  32. }
That's one way you can, since you asked. But you shouldn't. Private members are private for a reason, and you should respect that. Not to mention that if you have to ask about this, you're probably not aware of the many (many!) pitfalls and potential undefined behavior of what you want to do.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 20th, 2005
0

Re: Accessing Private data memebrs outside the class

Ok...thanx
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: plzzzzzzzzzzzzzzzzzz help
Next Thread in C++ Forum Timeline: C++ Time Conversion. HEEEEELP plz





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC