Accessing Private data memebrs outside the class

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Accessing Private data memebrs outside the class

 
0
  #1
Oct 20th, 2005
  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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,582
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Accessing Private data memebrs outside the class

 
0
  #2
Oct 20th, 2005
>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
  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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Accessing Private data memebrs outside the class

 
0
  #3
Oct 20th, 2005
Ok...thanx
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC